Bash: Useful commands for parsing
Bash: Useful commands for parsing

Bash: Useful commands for parsing

Some useful bash parsing commands

I hope to make constant updates in the post.

# Merge alternate rows from two files
paste -d \\n FILE1 FILE2

# Remove line breaks
tr '\n' ' ' INPUTfile > OUTfile # Replace 
tr --delete '\n' INPUTfile > OUTfile

# Select lines with NUM columns
awk -F "separator" 'NF==NUM{print}{}' INPUTfile > OUTfile

# Select lines with X value in Y column
awk -F "YOURSEP" '$Y == "X"' INPUTfile > OUTfile
# Select every N lines starting from M line in a file
sed -n 'M~Np' INPUTfile > OUTfile

## Select even lines
sed -n 'n;p' INPUTfile > OUTfile
sed -n 2~2p INPUTfile > OUTfile
awk 'NR%2==0' INPUTfile > OUTfile

## Select odd lines
sed -n 'p;n' INPUTfile > OUTfile
sed -n 1~2p INPUTfile > OUTfile
awk 'NR%2==0' INPUTfile > OUTfile

## Substitute with perl
perl -pe "s/[^0-9]\n//g" # Interestig case: Starts with number and ends with line break, remove line breaks

## Use variable in awk script
VARIABLE="Pattern"
awk -v var="$VARIABLE" 'BEGIN {print var}' # You can use different awk comands

## Use variable in sed
VAR="Pattern"
sed "s/$VAR/Aguacate/" # You must use double quotes (")