4

I have table in the current format:

Lead:Arrow1:Arrow2:Arrow3
Follow:Arrow4:Arrow5:Arrow6:Arrow7:Arrow8:Arrow9

I want to turn this to the

Lead
Arrow1
Arrow2
Arrow3
Follow
Arrow4
Arrow5
Arrow6
Arrow7
Arrow8
Arrow9

I tried awk:

$(awk -F":" '/Lead/ {print NF-1}' $f)
$(awk -F":" '/Follow/ {print NF-1}' $f)

but it didn't work. How can I do it in awk or any other method?

8

You can use tr as follows:

<file tr ":" "\n"

Lead
Arrow1
Arrow2
Arrow3
Follow
Arrow4
Arrow5
Arrow6
Arrow7
Arrow8
Arrow9

or awk as follows:

<file awk '{gsub(/:/,"\n")}1'
Lead
Arrow1
Arrow2
Arrow3
Follow
Arrow4
Arrow5
Arrow6
Arrow7
Arrow8
Arrow9
  • 2
    Nice, with GNU awk you could also write awk -v RS='[:\n]' 1 – user000001 Oct 9 '18 at 14:23
1

Using awk as you suggested:

$ awk '{ gsub(":", "\n", $0); print }' <file
Lead
Arrow1
Arrow2
Arrow3
Follow
Arrow4
Arrow5
Arrow6
Arrow7
Arrow8
Arrow9

This simply replaces each : in the input data with a newline.


Another awk solution which uses the OFS (Output Field Separator) variable:

awk -v OFS='\n' -F ':' '{ $1=$1; print }' <file

The assignment $1=$1 looks bogus but it forces awk to reformat the current input record according to OFS (and ORS, but we don't change that from the default, which is a newline), which in this case means inserting newlines between all :-delimited fields.


With awk, we could also just iterate over the :-delimited fields and print them individually:

awk -F ':' '{ for (i = 1; i <= NF; ++i) print $i }' <file
0

Another super-simple solution would be to convert the :s to spaces with sed, and then print the words with echo in a for loop:

for i in $(sed 's/:/ /g')
do
  echo $i
done

This script works from stdin to stdout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.