Removing columns in a text file using Bash?

I need to write a script that removes an Idle column from finger output.

>finger Login Name TTY Idle Login Time Office Phone Billy Billy Howard *con 6:55 Fri 19:03 Billy Billy Howard s00 5 Fri 19:11 Billy Billy Howard s00 Sat 00:27 

I tried removing extra spaces with tr and then using cut with a space delimiter to remove the column, but since Idle cannot have a value, I sometimes get the wrong value since tr divided the spaces into downtime ... Someone knows how can i delete an idle column?

+4
source share
3 answers

This solution is not perfect: the column position and width may change. If they are constant, the following command will do the trick by deleting the text columns 34 through 39 inclusive:

 finger | colrm 34 39 
+3
source

This might work for you:

 finger | sed 's/\(.\{35\}\)...../\1/' 

or that:

 finger | cut --complement -c36-40 
+7
source

If "\t" used as a column separator, you can get rid of the 4th column using awk and remove the double "\t" with sed . For instance:

 finger | awk -F"\t" -v 'OFS=\t' '{ $4=""; print $0}' | sed 's/\t\{2,\}/\t/' 
+3
source

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


All Articles