Cancel tabs / spaces

I used the grep command with sed and cut filters, which basically turn my output into something similar to this

this line 1 this line 2 another line 3 another line 4 

I am trying to get output without spaces between lines and before lines so that it looks like

  this line 1 this line 2 another line 3 another line 4 

I want to add more | filter

+6
source share
6 answers

Add this filter to remove the space from the beginning of the line and remove the empty lines, note that it uses two sed commands, one to remove leading spaces and the other to delete lines without content

 | sed -e 's/^\s*//' -e '/^$/d' 

The following is an example of a Wikipedia article for sed that uses the d command to delete lines that are empty or contain only spaces, my solution uses the \s escape sequence to match any space character (space, tab, etc.), here Wikipedia example:

 sed -e '/^ *$/d' inputFileName 
  • The carriage (^) matches the beginning of a line.
  • The dollar sign ($) matches the end of the line.
  • An asterisk (*) matches zero or more occurrences of the previous character.
+5
source

This can be done using the tr command. Thus

| tr -s [:space:]

or alternatively

| tr -s \\n

if you want to remove only line breaks, with no spaces at the beginning of each line.

+3
source

You can also use grep:

 ... | grep -o '[^$(printf '\t') ].*' 

Here we print lines that have at least one character that is not white. Using the "-o" flag, we print only a match, and we force the match to start the non-white space character.

EDIT: Changed command to remove leading space characters.

Hope this helps =)

+2
source

I would do it short and simple:

 sed 's: ::g' 

Add this at the end of your command and all spaces will be navels. For example, try this command:

 cat/proc/meminfo | sed 's: ::g' 
+2
source
 (whateverproducesthisoutput)|sed -E 's/^[[:space:]]+//'|grep -v '^$' 

(depending on your sed , you can replace [[:space:]] with \s ).

0
source

Use grep "^." filename grep "^." filename to remove blank lines during printing. Here, lines starting with any character match, so empty lines are not counted.

0
source

Source: https://habr.com/ru/post/927116/


All Articles