Issue
Suppose I have a text like this:
a b [c] d [e]
f [g]
[h]
i j
k [l]
I'd like to ask how to extract the strings between brackets line by line using sed or awk command?
The output should be:
c e
g
h
l
Edit: the following script seems to work.
while read -r line; do
echo "$line" | awk 'NR > 1 {printf "%s ", $1;}' RS='[' FS=']';
echo ;
done < file.txt;
Solution
This might work for you (GNU sed):
sed -r 's/[^[]*(\[([^]]*)\])*( *)[^[]*/\2\3/g' file
Answered By - potong Answer Checked By - Dawn Plyler (WPSolving Volunteer)