1

I have a text file; its content is like below.

$ cat file.txt
[] [1]foo1 bar1
[] [2]foo2 bar2
[] [35]foo3 bar3
[] [445]foo4 bar4
[] [87898]foo5 bar5

I can successfully remove the first column using awk, but I'm unable to remove [num] characters as it is associated with the string.

I'm trying to get a output like below

$ cat file.txt
foo1 bar1 
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
0

5 Answers 5

7
$ sed 's/.*]//' file.txt | tr -s ' '
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5

The sed removes everything on the line up to (and including) the final ], and the tr compresses multiple consecutive spaces into single spaces.

Alternatively, using only sed:

sed -e 's/.*]//' -e 's/  */ /g' file.txt

With the given input data, this produces the same output as the first pipeline.

This sed first does s/.*]// which deletes everything up to the ] (inclusive). The second expression matches ␣␣*, i.e. a space followed by zero or more spaces, and replaces these with a single space. The second expression is applied across the whole line and has the same effect as tr -s ' ', i.e. it compresses multiple consecutive spaces into single spaces.


Using awk:

awk -F '[][:blank:]]*' '{ print $3,$4 }' file.txt

Here, we use ] or spaces or tabs as field separators (multiples of these may separate two column, which is why we use * after the [...]). Given those separators, the wanted data is available in fields 3 and 4 on each line.


After the data in the question was edited to remove some spaces between the last two columns, the following will also do the job:

cut -d ']' -f 3 file.txt

alternatively just

sed 's/.*]//' file.txt

or

awk -F ']' '{ print $3 }' file.txt
2
  • Thanks again. I think in awk, we're missing [ before [:. After adding that it works fine. Other methods also working great. If possible could you please explain me the 2nd sed method? Thanks.
    – smc
    Commented Apr 16, 2018 at 14:35
  • 1
    @smc There is no [ missing. The [...] contains ] and [[:blank:]]. I will add an explanation of the second sed.
    – Kusalananda
    Commented Apr 16, 2018 at 14:41
1

You can use sed

$ sed -e 's/.*]//g' file
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5

You can use `awk

$ awk -F'.*]' '{print $2}' file
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
0

Using awk:

$ awk '{$2=substr($2,index($2,"]")+1);print $2,$3}' file
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5

The second parameter $2 is truncated using substr from the first ] found. The parameters 2 and 3 are then printed.

0

With cut

cut -d ] -f3 file.txt
0

Another awk proposal

awk '{gsub(/\[.+foo|\]/,"foo")}1' file
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .