I have a quick question regarding the AWK command. I need the command to print until the end of the line on the same line, but then when it gets to the next line I need it to print on another line. The following example will provide better clarity.
Say I have a file:
0 1 2 3 This is line one
0 1 2 3 This is line two
0 1 2 3 This is line three
0 1 2 3 This is line four
I have tried the following and gotten the following results
awk '{for(i=5;i<=NF;i++) print $i}' fileName >> resultsExample1
I get the following in resultsExample1
This
is
line
one
This
is
line
two
And so on....
Example 2:
awk 'BEGIN {" "} {for(i=5;i<=NF;i++) printf $1}' fileName >> resultsExample2
for resultsExample2 I get:
This is line one This is line two this is line three This is line four
I have also tried:
awk 'BEGIN {" "} {for(i=5;i<=NF;i++) printf $1}' fileName >> resultsExample3
But the results were the same as the previous one
In the end I want the following:
This is line one
This is line two
This is line three
This is line four
I'm grateful for any help! Thanks in advance :)
I know this question is very old, but another awk example:
awk '{print substr($0,index($0,$5))}' fileName
What it does: find the index where you want to start printing (index of $5 in $0) and print the substring of $0 starting at that index.
It may be more straight-forward to use cut
:
$ cut -d' ' -f5- file
This is line one
This is line two
This is line three
This is line four
This says: on space-separated fields, print from the 5th up to the end of the line.
If you happen to have multiple spaces in between fields, you may initially want to squeeze them with tr -s' '
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With