0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.