{"id":1592,"date":"2014-11-14T15:18:46","date_gmt":"2014-11-14T15:18:46","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1592"},"modified":"2014-11-14T15:18:46","modified_gmt":"2014-11-14T15:18:46","slug":"awk%e5%ad%a6%e4%b9%a0_4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1592.html","title":{"rendered":"AWK\u5b66\u4e60_4"},"content":{"rendered":"<h6>\u5982\u4f55\u7528awk\u5b9e\u73b0\u6253\u5370\u6307\u5b9a\u5217\u7684\u529f\u80fd\uff1f<\/h6>\n<p>Print all but the first three columns<br \/>\nToo cumbersome:<\/p>\n<pre class=\"lang:default decode:true\">awk '{print \" \"$4\" \"$5\" \"$6\" \"$7\" \"$8\" \"$9\" \"$10\" \"$11\" \"$12\" \"$13}' things<\/pre>\n<hr \/>\n<p>A solution that does not add extra leading or trailing whitespace:<\/p>\n<pre class=\"lang:default decode:true\">awk '{for(i=4;i&lt;NF;i++)printf \"%s\",$i OFS; if (NF) printf \"%s\",$NF; printf ORS}'<\/pre>\n<p>Demo:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{for(i=4;i&lt;NF;i++) printf\"%s\",$i OFS;if(NF)printf\"%s\",$NF;printf ORS}' |\n  tr ' ' '-'\n4-5-6-7<\/pre>\n<p>Another approach using the ternary operator is Sudo_O&#8217;s solution:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{for(i=4;i&lt;=NF;i++)printf \"%s\",$i (i==NF?ORS:OFS)}' | tr ' ' '-'\n4-5-6-7<\/pre>\n<p>And EdMorton gives a solution that preserves the original whitespace between fields:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{sub(\/([^ ]+ +){3}\/,\"\")}1' | tr ' ' '-'\n4---5----6-7<\/pre>\n<p>The solution given by larsr in the comments is almost correct:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{for (i=3;i&lt;=NF;i++) $(i-2)=$i; NF=NF-2; print $0}' | tr ' ' '-'\n3-4-5-6-7<\/pre>\n<p>This is the fixed and parametrized version of larsr solution:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{for(i=n;i&lt;=NF;i++)$(i-(n-1))=$i;NF=NF-(n-1);print $0}' n=4 | tr ' ' '-'\n4-5-6-7<\/pre>\n<p>All other answers are nice but add extra spaces:<\/p>\n<p style=\"padding-left: 30px;\">Example of answer adding extra leading spaces:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' | awk '{$1=$2=$3=\"\"}1' | tr ' ' '-'\n---4-5-6-7<\/pre>\n<p style=\"padding-left: 30px;\">Example of answer adding extra trailing space<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6 7' |\n  awk '{for(i=4;i&lt;=13;i++)printf \"%s \",$i;printf \"n\"}' file | tr ' ' '-'\n4-5-6-7-<\/pre>\n<p>====<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{for(i=1;i&lt;4;i++) $i=\"\";print}' file<\/pre>\n<p>use cut<\/p>\n<pre class=\"lang:default decode:true\">$ cut -f4-13 file<\/pre>\n<p>or if you insist on awk and $13 is the last field<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{$1=$2=$3=\"\";print}' file<\/pre>\n<p>else<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{for(i=4;i&lt;=13;i++)printf \"%s \",$i;printf \"n\"}' file<\/pre>\n<p>====<br \/>\nThe correct way to do this is with an RE interval because it lets you simply state how many fields to skip, and retains inter-field spacing for the remaining fields.<\/p>\n<p>e.g. to skip the first 3 fields without affecting spacing between remaining fields given the format of input we seem to be discussing in this question is simply:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1 2 3 4 5 6' | awk '{sub(\/([^ ]+ +){3}\/,\"\")}1'\n4 5 6<\/pre>\n<p>If you want to accommodate leading spaces and non-blank spaces, but again with the default FS, then it&#8217;s:<\/p>\n<pre class=\"lang:default decode:true\">$ echo ' 1 2 3 4 5 6' | awk '{sub(\/[[:space:]]*([^[:space:]]+[[:space:]]+){3}\/,\"\")}1'\n4 5 6<\/pre>\n<p>If you have an FS that&#8217;s an RE you can&#8217;t negate in a character set, you can convert it to a single char first (RS is ideal if it&#8217;s a single char since an RS CANNOT appear within a field, otherwise consider SUBSEP), then apply the RE interval subsitution, then convert to the OFS. e.g. if chains of &#8220;.&#8221;s separated the fields:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1...2.3.4...5....6' | awk -F'[.]+' '{gsub(FS,RS);sub(\"([^\"RS\"]+[\"RS\"]+){3}\",\"\");gsub(RS,OFS)}1'\n4 5 6<\/pre>\n<p>Obviously if OFS is a single char AND it can&#8217;t appear in the input fields you can reduce that to:<\/p>\n<pre class=\"lang:default decode:true\">$ echo '1...2.3.4...5....6' | awk -F'[.]+' '{gsub(FS,OFS); sub(\"([^\"OFS\"]+[\"OFS\"]+){3}\",\"\")}1'\n4 5 6<\/pre>\n<p>Then you have the same issue as with all the loop-based solutions that reassign the fields &#8211; the FSs are converted to OFSs. If that&#8217;s an issue, you need to look into GNU awks&#8217; patsplit() function.<\/p>\n<p>====<br \/>\nPretty much all the answers currently add either leading spaces, trailing spaces or some other separator issue. To select from the fourth field where the separator is whitespace and the output separator is a single space using awk would be:<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{for(i=4;i&lt;=NF;i++)printf \"%s\",$i (i==NF?ORS:OFS)}' file<\/pre>\n<p>To parametrize the starting field you could do:<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{for(i=n;i&lt;=NF;i++)printf \"%s\",$i (i==NF?ORS:OFS)}' n=4 file<\/pre>\n<p>And also the ending field:<\/p>\n<pre class=\"lang:default decode:true\">$ awk '{for(i=n;i&lt;=m=(m&gt;NF?NF:m);i++)printf \"%s\",$i (i==m?ORS:OFS)}' n=4 m=10 file<\/pre>\n<h6>\u5b9e\u9645\u6d4b\u8bd5\u6548\u679c\uff1a<\/h6>\n<pre class=\"lang:default decode:true\"># echo '1 2 3 4 5 6 7'\n1 2 3 4 5 6 7\n# echo '1 2 3 4 5 6 7' | awk '{for(i=1;i&lt;4;i++) $i=\"\";print}'\n4 5 6 7\n# echo '1 2 3 4 5 6 7' | awk '{$1=$2=$3=\"\";print}'\n4 5 6 7\n#\n# echo '1 2 3 4 5 6 7' | awk '{for(i=4;i&lt;NF;i++)printf \"%s\",$i OFS; if (NF) printf \"%s\",$NF; printf ORS}'\n4 5 6 7\n#\n# echo '1 2 3 4 5 6 7' | awk '{sub(\/([^ ]+ +){3}\/,\"\")}1'\n4 5 6 7\n#\n# echo '1 2 3 4 5 6 7' | awk '{ for (i=3; i&lt;=NF; i++) print $i }'\n3\n4\n5\n6\n7\n# echo '1 2 3 4 5 6 7' | awk '{$1=$2=$3=\"\"}sub(\"^\"FS\"*\",\"\")'\n4 5 6 7<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/2626274\/print-all-but-the-first-three-columns\" target=\"_blank\">Print all but the first three columns<\/a><\/p>\n<p>==<\/p>\n<h6>\u5982\u4f55\u5220\u9664\u8f93\u51fa\u4e2d\u7684\u524d\/\u540e\u5bfc\u7a7a\u683c\uff08\u7c7b\u4f3c\u4e8etrim\u7684\u4f5c\u7528\uff09\uff1f<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/bash-remove-whitespace-from-string\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/faq\/bash-remove-whitespace-from-string\/<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/sed-tip-delete-all-blank-white-spaces\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/faq\/sed-tip-delete-all-blank-white-spaces\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/369758\/how-to-trim-whitespace-from-bash-variable\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/369758\/how-to-trim-whitespace-from-bash-variable<\/a><\/li>\n<\/ul>\n<p>This will remove all spaces &#8230;\uff08\u5220\u9664\u6240\u6709\u7a7a\u683c\uff09<\/p>\n<pre class=\"lang:default decode:true\"># echo \" test test test \" | tr -d ' '\ntesttesttest<\/pre>\n<p>This will remove trailing spaces&#8230;\uff08\u5220\u9664\u5c3e\u90e8\u7a7a\u683c\uff09<\/p>\n<pre class=\"lang:default decode:true\"># echo \" test test test \" | sed 's\/ *$\/\/'\n test test test<\/pre>\n<p>This will remove leading spaces&#8230;\uff08\u5220\u9664\u524d\u5bfc\u7a7a\u683c\uff09<\/p>\n<pre class=\"lang:default decode:true\"># echo \" test test test \" | sed 's\/^ *\/\/'\ntest test test<\/pre>\n<p>This will remove both trailing and leading spaces\uff08\u540c\u65f6\u5220\u9664\u524d\u540e\u7a7a\u683c\uff09<\/p>\n<pre class=\"lang:default decode:true\"># echo \" test test test \" | sed -e 's\/^ *\/\/' -e 's\/ *$\/\/'\ntest test test<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/strong><\/span><a href=\"http:\/\/search.aol.com\/aol\/search?q=linux+how+to+trim+whitespace+of+output\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=linux+how+to+trim+whitespace+of+output<\/a><\/p>\n<p>==<\/p>\n<h5>\u7528awk\u53bb\u91cd\uff08\u83b7\u53d6\u4e24\u79cd\u7c7b\u578b\u7684\u53bb\u91cd\u6570\u636e\u7684\u65b9\u6cd5\uff09<\/h5>\n<pre class=\"lang:default decode:true\">array2=$(cat filename.txt | awk '{type[\"all\"]+=$3; type[$2]+=$3} END {for (i in type) print i, type[i]}')<\/pre>\n<p>\u8bbe\u7f6e\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f46\u662f\u7528\u4e0d\u540c\u7684\u6570\u7ec4\u4e0b\u6807\u8fdb\u884c\u533a\u5206\uff08\u6bd4\u5982\u8fd9\u91cc\u7684\uff1aall \u548c $2 \uff0c\u53ea\u662f\u4e0b\u6807\u4e0d\u540c\uff0c\u4f46\u7528\u7684\u540c\u4e00\u4e2a\u6570\u7ec4\uff09\uff0c\u6700\u540e\u7528\u4e2aEND\u8fdb\u884c\u6253\u5370\u5373\u53ef\uff1b\u5f53\u7136\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u6570\u7ec4\uff0c\u4f46\u5982\u4f55\u5173\u8054\u8d77\u6765\u5c31\u662f\u4e2a\u6280\u5de7\u95ee\u9898\u4e86\uff08\u8ddf\u5177\u4f53\u9700\u6c42\u76f8\u5173\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528awk\u5b9e\u73b0\u6253\u5370\u6307\u5b9a\u5217\u7684\u529f\u80fd\uff1f Print all but the first three columns [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11],"tags":[74],"class_list":["post-1592","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-awk"],"views":2629,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1592"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1592\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}