1

from this:

1

2

3

4

5

6

The result should be like this:

1
2
3
4
5
6
  • 2
    Use code formatting for input and output, please: unix.stackexchange.com/editing-help#code – muru Oct 27 '19 at 5:18
  • 2
    Why not use this, sed '/^$/d' 1-6.txt | tr '\n' ' '? – jbrock Oct 27 '19 at 5:35
  • 2
    Or just grep . which matches non-empty lines only. – Ralph Rönnquist Oct 27 '19 at 6:53
  • Rather than adding a separate "Thank you", upvote the answer(s) that helped you, and consider "accepting" the answer that was the most helpful in resolving your issue. These are the best ways of showing gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers – Kusalananda Oct 27 '19 at 9:58
2

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
1

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.

1

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).

  • Thanks a lot for the help – Yuri Diculous Oct 27 '19 at 8:51
1

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.

1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.