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.
OR with awk
awk '{$1=$2=$3=$4=""; sub(/^ */,"", $0); print }' awkTest2.txt
This is line one
This is line two
This is line three
This is line four
Also, you're solution is almost there, you just need to force a '\\n' to be printed at the end of each processed line, ie
awk '{for(i=5;i<=NF;i++) {printf $i " "} ; printf "\n"}' awkTest2.txt
This is line one
This is line two
This is line three
This is line four
Note that your BEGIN { " " }
is a no op. And you should use $i
instead of $1
to print the current iterations value.
IHTH.
Edit ; Noting sudo_O objection, I added a %s to the data. Here is the output
This is line one
This is line two
This is line three
T%shis is line four
This may be a problem for you, so it that case read about how to pass a format string to printf.
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' '
.
awk '{gsub (/[[:digit:]]/,"");{$1=$1}}1' file
sed
provides the best solution to this problem .
The accepted cut-based solution has the problem that, unlike awk, it assumes there is exactly one space between fields.
The usual fix of using tr -s ' '
to squeeze multiple adjacent spaces into one space is problematic too: it would collapse spaces in the trailing remainder of the line, thus modifying it, as @inopinatus commented.
The following sed-based solution will achieve our goal while preserving spaces in the remainder of the line:
sed -E 's/^([^ \t]*[ \t]*){4}//' <<'EOF'
0 1 2 3 This is line one
0 1 2 3 This is line two test of extra spaces
0 1 2 3 This is line three
0 1 2 3 This is line four
EOF
Result:
This is line one
This is line two test of extra spaces
This is line three
This is line four
We simulated awk's default behavior of delimiting fields by sequences of whitespace.
Fields are normally separated by whitespace sequences (spaces, TABs, and newlines)
– Default Field Splitting (The GNU Awk User's Guide)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.