Use awk to print $0 using the same format for all columns
Considering this input:
$ a=$'0.1 35 23e3\n0.2 36 24e3';echo "$a"
0.1 35 23e3
0.2 36 24e3
This gnu awk will achieve what you expect without looping over the fields:
$ echo "$a" |awk '{printf "%.2e%s",$0,RT}' RS="[ ]|\n"
1.00e-01 3.50e+01 2.30e+04
2.00e-01 3.60e+01 2.40e+04
I have intentionally exclude 4.0D+03
since does not seem valid.
Using awk to print all columns from the nth to the last
Print all columns:
awk '{print $0}' somefile
Print all but the first column:
awk '{$1=""; print $0}' somefile
Print all but the first two columns:
awk '{$1=$2=""; print $0}' somefile
AWK: Preserving the format when printing print $0
Reassigning back to $0 is what you're after:
awk '/ EG = / {$0 = sprintf(" %7.5E%s", 0.5*($1+$7), substr($0,13))} 1' mixed_file
awk print $0 with newline separated column values
Just trying out awk
echo "PREFIX_foo,PREFIX_bar" | awk -F, -v OFS="\n" '{gsub(/PREFIX_/,""); $1=$1}1'
using awk or sed to print all columns from the n-th to the last
To preserve whitespace in awk, you'll have to use regular expression substitutions or use substrings. As soon as you start modifying individual fields, awk has to recalculate $0 using the defined (or implicit) OFS.
Referencing Tom's sed answer:
awk '{sub(/^([^[:blank:]]+[[:blank:]]+){1}/, "", $0); print}' 1.txt
How to print all the columns after a particular number using awk?
awk '{ s = ""; for (i = 9; i <= NF; i++) s = s $i " "; print s }'
Print all but the first three columns
A solution that does not add extra leading or trailing whitespace:
awk '{ for(i=4; i<NF; i++) printf "%s",$i OFS; if(NF) printf "%s",$NF; printf ORS}'
### Example ###
$ echo '1 2 3 4 5 6 7' |
awk '{for(i=4;i<NF;i++)printf"%s",$i OFS;if(NF)printf"%s",$NF;printf ORS}' |
tr ' ' '-'
4-5-6-7
Sudo_O proposes an elegant improvement using the ternary operator NF?ORS:OFS
$ echo '1 2 3 4 5 6 7' |
awk '{ for(i=4; i<=NF; i++) printf "%s",$i (i==NF?ORS:OFS) }' |
tr ' ' '-'
4-5-6-7
EdMorton gives a solution preserving original whitespaces between fields:
$ echo '1 2 3 4 5 6 7' |
awk '{ sub(/([^ ]+ +){3}/,"") }1' |
tr ' ' '-'
4---5----6-7
BinaryZebra also provides two awesome solutions:
(these solutions even preserve trailing spaces from original string)
$ echo -e ' 1 2\t \t3 4 5 6 7 \t 8\t ' |
awk -v n=3 '{ for ( i=1; i<=n; i++) { sub("^["FS"]*[^"FS"]+["FS"]+","",$0);} } 1 ' |
sed 's/ /./g;s/\t/->/g;s/^/"/;s/$/"/'
"4...5...6.7.->.8->."
$ echo -e ' 1 2\t \t3 4 5 6 7 \t 8\t ' |
awk -v n=3 '{ print gensub("["FS"]*([^"FS"]+["FS"]+){"n"}","",1); }' |
sed 's/ /./g;s/\t/->/g;s/^/"/;s/$/"/'
"4...5...6.7.->.8->."
The solution given by larsr in the comments is almost correct:
$ echo '1 2 3 4 5 6 7' |
awk '{for (i=3;i<=NF;i++) $(i-2)=$i; NF=NF-2; print $0}' | tr ' ' '-'
3-4-5-6-7
This is the fixed and parametrized version of larsr solution:
$ echo '1 2 3 4 5 6 7' |
awk '{for(i=n;i<=NF;i++)$(i-(n-1))=$i;NF=NF-(n-1);print $0}' n=4 | tr ' ' '-'
4-5-6-7
All other answers before Sep-2013 are nice but add extra spaces:
Example of answer adding extra leading spaces:
$ echo '1 2 3 4 5 6 7' |
awk '{$1=$2=$3=""}1' |
tr ' ' '-'
---4-5-6-7Example of answer adding extra trailing space
$ echo '1 2 3 4 5 6 7' |
awk '{for(i=4;i<=13;i++)printf "%s ",$i;printf "\n"}' |
tr ' ' '-'
4-5-6-7-------
print all but select fields in awk
Your first try was pretty close. Modifying it to use printf
and including the field separators worked for me:
awk '{printf $1FS$2; for (i=5; i <= NF; i++) printf FS$i; print NL }'
AWK format data based on columns with multiple rows
Try doing this :
awk '
BEGIN{OFMT="INSERT INTO `table` \
VALUES(\n\t`%s`,\n\t`%s`,\n\t`%s`,\n\t`%s`\n);\n"
}
NF==3{printf OFMT, $1, $2, $3, $4v}
NF==4{if (length(v)){printf OFMT, $1, $2, $3, $4v;v=""}
else{printf OFMT, $1, $2, $3, $4}
}
NF==1{v=v","$1}
' file | mysql
Output without mysql
INSERT INTO `table` VALUES(
`TEST1_Core`,
`990010:10011608`,
`ipv4`,
`Vl1162`
);
INSERT INTO `table` VALUES(
`AA2_Autism_Core`,
`990010:10014478`,
`ipv4`,
`Vl1447`
);
INSERT INTO `table` VALUES(
`6753312_Core`,
`990010:1004868`,
`ipv4`,
``
);
INSERT INTO `table` VALUES(
`542343423`,
`990010:1004128`,
`ipv4`,
``
);
INSERT INTO `table` VALUES(
`Bgdfdfgdf`,
`990010:2728`,
`ipv4`,
``
);
INSERT INTO `table` VALUES(
`gfgCore`,
`990010:1002108`,
`ipv4`,
`Vl2105`
);
INSERT INTO `table` VALUES(
`fgdfgfgdfg_Core`,
`990010:10021038`,
`ipv4`,
`Vl2103`
);
INSERT INTO `table` VALUES(
`42342342342342Core`,
`990010:2105898`,
`ipv4`,
`Vl664`
);
INSERT INTO `table` VALUES(
`24234234N_Core`,
`990010:1007967`,
`ipv4`,
`Vl2552,Vl896`
);
INSERT INTO `table` VALUES(
`C86765Core`,
`990010:1001708`,
`ipv4`,
`Vl905`
);
INSERT INTO `table` VALUES(
`Dhyhyh_Core`,
`990010:100106`,
`ipv4`,
``
);
INSERT INTO `table` VALUES(
`ghfghfhdfCore`,
`990010:1009418`,
`ipv4`,
`Vl941`
);
INSERT INTO `table` VALUES(
`hfghfghdf11`,
`990010:10008`,
`ipv4`,
`,Vl1028`
);
INSERT INTO `table` VALUES(
`yreyryer-12`,
`990010:20002`,
`ipv4`,
`Vl749,Vl1028`
);
INSERT INTO `table` VALUES(
`42342342_Core`,
`990010:1004068`,
`ipv4`,
`Vl50,Vl874,Vl894,Vl942,Vl1172,Vl1439,Vl2553`
);
INSERT INTO `table` VALUES(
`gdfgdg_Core`,
`990010:1004498`,
`ipv4`,
`Vl617`
);
INSERT INTO `table` VALUES(
`Spgdfggdf`,
`990010:1002798`,
`ipv4`,
`Vl779`
);
INSERT INTO `table` VALUES(
`gdgdgdgdgCore`,
`990010:1004278`,
`ipv4`,
``
);
INSERT INTO `table` VALUES(
`test`,
`990010:500500`,
`ipv4`,
``
);
Related Topics
Selinux Prevented Httpd(Usr/Sbin/Httpd) Write Access to /Var/Www/HTML/Bookings/Templates_C
Implementation of Function Execve (Unistd.H)
Tar: Cowardly Refusing to Create an Empty Archive
Sublime Text 2 Build (Ctrl +B) Intel Fortran Compiler
How to Enable or Configure Ftrace Module
Change The Escape Sequence Generated by Xterm for Key Combinations
Insecure $Env{Path} While Running with - T Switch
Three Dots Directory Traversal with Mv
Bash: Transform Key-Value Lines to CSV Format
Possible to Assign a New Ip Address on Every Http Request
Host Multiple ASP.NET Core Web Application Under a Single Linux Server
Generic Printing Using a Usb Port
Vue Npm Run Serve Failed to Load Resource: Net::Err_Content_Length_Mismatch
Floating Point Rounding in Shell
Unexpected Eof While Looking for Matching '"'