I am trying to get rid of all the empty lines from a file but I do want to preserve "\n" after every non empty line.
Problem: command works correctly if used in CLI but as soon as I use any command in a bash script it removes all "\n" so I have all my results in one line instead of having them in separate lines.
here is my code:
#printing second and third word from every line and remove lines that do not contain any digits
result=$(cat "$output_file" | awk '{print $2" "$3}' | sed 's/[^0-9]*/\\n/')
echo -e ""$result"" > "$output_file"
#getting rid of all empty lines but what happens is that the whole file becomes one line
no_empty_lines=$(cat "$output_file" | awk NF)
echo -e ""$no_empty_lines"" > "$output_file"
file to edit:
> 135.121.62.246 7.4 > 135.121.160.65 7.8 > 135.121.106.56 7.5 > > > 135.121.106.96 6.2 > > > 135.121.160.106 10 > > 135.121.90.46 commandFailed
demanded result:
file to edit:
> 135.121.46.246 7.4 > 135.121.106.46 7.8 > 135.121.106.56 7.5 > 135.121.106.96 6.2 > 135.121.160.16 10 > 135.121.90.46 commandFailed
>
signs part of the file data or just part of the (broken) representation here? Because you're referring to fields$2
and$3
in the awk code? – ilkkachu Dec 29 '20 at 15:20