Remove certain characters in a text file
Solution 1
$ 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
Solution 2
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
smc
Updated on September 18, 2022Comments
-
smc over 1 year
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
-
smc about 6 yearsThanks 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.
-
Kusalananda about 6 years@smc There is no
[
missing. The[...]
contains]
and[[:blank:]]
. I will add an explanation of the secondsed
.