Reputation: 1
I have a complete list of names separated by commas:
second_surnameA first_surnameA, nameA second_surnameB first_surnameB, nameB second_surnameC first_surnameC, nameC ....
So I'm trying to capture with a bash script the name of all persons (A, B, C) in each file:
second_surnameA first_surnameA, nameA
second_surnameB first_surnameB, nameB
second_surnameC first_surnameC, nameC
I executed:
cat names_file.txt | tr ',' '\n'
is almost what I need but not enouth. Any idea?
Upvotes: 0
Views: 64
Reputation: 7657
With GNU
sed:
sed -re 's/(\S+ +){3}/&\n/g' input_file | sed 's/ $//' > output_file
The pattern (\S+ +){3}
matches three times {3}
a group made of: one or more non-spaces \S+
followed by one or more spaces. The replacement string &\n
appends a new line to each matched pattern. The second sed removes trailing spaces and writes to output_file
Upvotes: 1
Reputation: 4004
With awk
:
awk '{for (i=1;i<=NF;i=i+3) print $i,$(i+1),$(i+2)}' inputfile
It simply uses blank spaces as delimiters to each field and prints three of them for each line.
Upvotes: 1