Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK command to print until end of line

Tags:

shell

unix

awk

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 :)

like image 996
ola Avatar asked Jun 13 '13 15:06

ola


2 Answers

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.

like image 85
Dani_l Avatar answered Oct 15 '22 23:10

Dani_l


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' '.

like image 27
fedorqui 'SO stop harming' Avatar answered Oct 15 '22 23:10

fedorqui 'SO stop harming'