from this:
1
2
3
4
5
6
The result should be like this:
1
2
3
4
5
6
from this:
1
2
3
4
5
6
The result should be like this:
1
2
3
4
5
6
This will just remove blank lines. You can remove the "file" if it's just output that you're operating on:
sed '/^[[:space:]]*$/d' file
That operates on all lines starting with, containing, and ending with spaces.
If it's a file that you want to edit directly instead of sending it to standard output:
sed -i '/^[[:space:]]*$/d' file
Output
1
2
3
4
5
6
cut
command is not the right tool for this requirements but however we can build a command to get desired result (or the tools you should use instead):
<infile cut -d$'\n' -f1$(printf '%s' ,{3..18..2})
{min..max..step}
is bash brace-expansion that produces ,3,5,7,9,11,...
output (generate odd fields numbers to cut's -f
option -f1,3,5,7,11,...
); select max with a big number if your input file's number of lines is unknown.
this only select the odd lines, so it's not skipping empty lines you think.
cut
is primarily a tool for extracting particular columns from data (the opposite of what paste
does). It is therefor probably not the right tool for removing/extracting rows of text.
GNU sed
can be used to delete ever second line starting with line 2:
$ sed '2~2d' file
1
2
3
4
5
6
... or to print every second line starting at line 1 (ignoring the others):
$ sed -n '1~2p' file
1
2
3
4
5
6
The x~y
address range refers to line x
, and then every y
line after that (this is a GNU sed
extension to standard sed
).
You didn’t mention where the data comes from.
If it’s a file:
grep . yourfile
If it’s another command:
yourcommand | grep .
This works by asking grep
to grab all lines which contain at least one character. But if your blank lines have white space on them, it won’t work. You would need to amend as follows:
grep -vE "^\s+$"
If you have a mix of blank and white space, you can chain the commands together.
awk NF
is all you need. NF is number of fields, as delineated by whitespace. Zero is a false boolean, so those lines are not printed.
sed '/^$/d' 1-6.txt | tr '\n' ' '
? – jbrock Oct 27 '19 at 05:35grep .
which matches non-empty lines only. – Ralph Rönnquist Oct 27 '19 at 06:53