打印除前三列之外的所有列

发布于 2024-08-28 19:11:18 字数 114 浏览 5 评论 0原文

太麻烦了:

awk '{print " "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13}' things

Too cumbersome:

awk '{print " "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13}' things

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

生死何惧 2024-09-04 19:11:19

对我来说,该请求的最紧凑和最合规的解决方案是

$ a='1   2\t \t3     4   5   6 7 \t 8\t '; 
$ echo -e "$a" | awk -v n=3 '{while (i<n) {i++; sub($1 FS"*", "")}; print $0}'

如果您有更多行要处理,例如文件 foo.txt,请不要忘记将 i 重置为 0:

$ awk -v n=3 '{i=0; while (i<n) {i++; sub($1 FS"*", "")}; print $0}' foo.txt

感谢您的论坛。

For me the most compact and compliant solution to the request is

$ a='1   2\t \t3     4   5   6 7 \t 8\t '; 
$ echo -e "$a" | awk -v n=3 '{while (i<n) {i++; sub($1 FS"*", "")}; print $0}'

And if you have more lines to process as for instance file foo.txt, don't forget to reset i to 0:

$ awk -v n=3 '{i=0; while (i<n) {i++; sub($1 FS"*", "")}; print $0}' foo.txt

Thanks your forum.

清醇 2024-09-04 19:11:19

由于我对第一个高度赞成但错误的答案感到恼火,我发现足够的东西可以在那里写一个回复,这里错误的答案被标记为这样,这是我的部分。我不喜欢建议的解决方案,因为我认为没有理由让答案如此复杂。

我有一个日志,其中 5 美元之后的 IP 地址可以是更多文本或没有文本。如果 5 美元之后有任何内容,我需要从 IP 地址到行尾的所有信息。就我而言,这实际上是一个 awk 程序,而不是 awk oneliner,所以 awk 必须解决这个问题。当我尝试使用旧的漂亮且最受好评但完全错误的答案删除前 4 个字段时:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218 one two three" | awk '{$1=$2=$3=$4=""; printf "[%s]\n", $0}'

它会吐出错误且无用的响应(我添加了 [] 来演示):

[    37.244.182.218 one two three]

相反,如果列在切点之前是固定宽度的,并且需要 awk,正确且非常简单的答案是:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218 one two three" | awk '{printf "[%s]\n", substr($0,28)}'

它会产生所需的输出:

[37.244.182.218 one two three]

As I was annoyed by the first highly upvoted but wrong answer I found enough to write a reply there, and here the wrong answers are marked as such, here is my bit. I do not like proposed solutions as I can see no reason to make answer so complex.

I have a log where after $5 with an IP address can be more text or no text. I need everything from the IP address to the end of the line should there be anything after $5. In my case, this is actualy withn an awk program, not an awk oneliner so awk must solve the problem. When I try to remove the first 4 fields using the old nice looking and most upvoted but completely wrong answer:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218 one two three" | awk '{$1=$2=$3=$4=""; printf "[%s]\n", $0}'

it spits out wrong and useless response (I added [] to demonstrate):

[    37.244.182.218 one two three]

Instead, if columns are fixed width until the cut point and awk is needed, the correct and quite simple answer is:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218 one two three" | awk '{printf "[%s]\n", substr($0,28)}'

which produces the desired output:

[37.244.182.218 one two three]
梦途 2024-09-04 19:11:19

我发现了另一种可能性,也许它也有用...

awk 'BEGIN {OFS=ORS="\t" }; {for(i=1; i<14; i++) 打印 $i " "; print $NF "\n" }' your_file

注意: 1. 对于表格数据以及 $1 到 $14 列

I've found this other possibility, maybe it could be useful also...

awk 'BEGIN {OFS=ORS="\t" }; {for(i=1; i<14; i++) print $i " "; print $NF "\n" }' your_file

Note: 1. For tabular data and from column $1 to $14

書生途 2024-09-04 19:11:19

这与之前的一些答案相差不远,但确实解决了几个问题:

cols.sh

#!/bin/bash
awk -v s=$1 '{for(i=s; i<=NF;i++) printf "%-5s", $i; print "" }'

您现在可以使用将成为起始列的参数来调用它:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 3 
3    4    5    6    7    8    9    10   11   12   13   14

或者:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 7 
7    8    9    10   11   12   13   14

这是 1 索引的;如果您更喜欢零索引,请改用 i=s + 1

此外,如果您想要起始索引结束索引的参数,请将文件更改为:

#!/bin/bash
awk -v s=$1 -v e=$2 '{for(i=s; i<=e;i++) printf "%-5s", $i; print "" }'

例如:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 7 9 
7    8    9

%-5s 将结果对齐为 5-字符宽的列;如果这还不够,请增加数量,或者如果您不关心对齐方式,请使用 %s (带空格)。

This isn't very far from some of the previous answers, but does solve a couple of issues:

cols.sh:

#!/bin/bash
awk -v s=$1 '{for(i=s; i<=NF;i++) printf "%-5s", $i; print "" }'

Which you can now call with an argument that will be the starting column:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 3 
3    4    5    6    7    8    9    10   11   12   13   14

Or:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 7 
7    8    9    10   11   12   13   14

This is 1-indexed; if you prefer zero indexed, use i=s + 1 instead.

Moreover, if you would like to have to arguments for the starting index and end index, change the file to:

#!/bin/bash
awk -v s=$1 -v e=$2 '{for(i=s; i<=e;i++) printf "%-5s", $i; print "" }'

For example:

$ echo "1 2 3 4 5 6 7 8 9 10 11 12 13 14" | ./cols.sh 7 9 
7    8    9

The %-5s aligns the result as 5-character-wide columns; if this isn't enough, increase the number, or use %s (with a space) instead if you don't care about alignment.

差↓一点笑了 2024-09-04 19:11:19

基于 AWK printf 的解决方案可避免 % 问题,并且独特之处在于,如果要打印的列少于 4 列,则它不会返回任何内容(无返回字符):

awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'

测试:

$ x='1 2 3 %s 4 5 6'
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
%s 4 5 6
$ x='1 2 3'
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
$ x='1 2 3 '
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
$

AWK printf-based solution that avoids % problem, and is unique in that it returns nothing (no return character) if there are less than 4 columns to print:

awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'

Testing:

$ x='1 2 3 %s 4 5 6'
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
%s 4 5 6
$ x='1 2 3'
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
$ x='1 2 3 '
$ echo "$x" | awk 'NF > 3 { for(i=4; i<NF; i++) printf("%s ", $(i)); print $(i) }'
$
嘿咻 2024-09-04 19:11:19

使用 cut:

cut -d <The character between characters> -f <number of first column>,<number of last column> <file name>

例如:如果您的 file1 包含:car.is.nice.equal.bmw

运行:cut -d 。 -f1,3 file1 将打印 car.is.nice

Use cut:

cut -d <The character between characters> -f <number of first column>,<number of last column> <file name>

e.g.: If you have file1 containing : car.is.nice.equal.bmw

Run : cut -d . -f1,3 file1 will print car.is.nice

夢归不見 2024-09-04 19:11:18
awk '{for(i=1;i<4;i++) $i="";print}' file
awk '{for(i=1;i<4;i++) $i="";print}' file
遗弃M 2024-09-04 19:11:18

使用 cut

$ cut -f4-13 file

或者如果您坚持使用 awk 并且 $13 是最后一个

$ awk '{$1=$2=$3="";print}' file

字段

$ awk '{for(i=4;i<=13;i++)printf "%s ",$i;printf "\n"}' file

use cut

$ cut -f4-13 file

or if you insist on awk and $13 is the last field

$ awk '{$1=$2=$3="";print}' file

else

$ awk '{for(i=4;i<=13;i++)printf "%s ",$i;printf "\n"}' file
烟柳画桥 2024-09-04 19:11:18

不添加额外前导或尾随空白的解决方案:

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 提出了使用三元运算符 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 给出了保留字段之间原始空格的解决方案:

$ echo '1   2 3 4   5    6 7' |
  awk '{ sub(/([^ ]+ +){3}/,"") }1' |
  tr ' ' '-'
4---5----6-7

BinaryZebra 还提供了两个很棒的解决方案:
(这些解决方案甚至保留原始字符串的尾随空格)

$ 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->."

评论中 larsr 给出的解决方案几乎是正确:

$ 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

这是 larsr 解决方案的固定和参数化版本:

$ 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

2013 年 9 月之前的所有其他答案都很好,但添加了额外的空格:

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:

乄_柒ぐ汐 2024-09-04 19:11:18

试试这个:

awk '{ $1=""; $2=""; $3=""; print $0 }'

Try this:

awk '{ $1=""; $2=""; $3=""; print $0 }'
薄情伤 2024-09-04 19:11:18

正确的方法是使用 RE 间隔,因为它可以让您简单地说明要跳过的字段数,并保留剩余字段的字段间间距。

例如,考虑到我们在这个问题中讨论的输入格式,要跳过前 3 个字段而不影响剩余字段之间的间距很简单:

$ echo '1   2 3 4   5    6' |
awk '{sub(/([^ ]+ +){3}/,"")}1'
4   5    6

如果您想容纳前导空格和非空白空格,但再次使用默认 FS,那么它是:

$ echo '  1   2 3 4   5    6' |
awk '{sub(/[[:space:]]*([^[:space:]]+[[:space:]]+){3}/,"")}1'
4   5    6

如果你有一个 FS,它是一个 RE,你不能在字符集中取反,你可以先将它转换为单个字符(如果它是单个字符,RS 是理想的,因为 RS 不能出现在字段中,否则请考虑 SUBSEP ),然后应用RE间隔替换,然后转换为OFS。例如,如果“.”链分隔字段:

$ echo '1...2.3.4...5....6' |
awk -F'[.]+' '{gsub(FS,RS);sub("([^"RS"]+["RS"]+){3}","");gsub(RS,OFS)}1'
4 5 6

显然,如果 OFS 是单个字符并且它不能出现在输入字段中,您可以将其简化为:

$ echo '1...2.3.4...5....6' |
awk -F'[.]+' '{gsub(FS,OFS); sub("([^"OFS"]+["OFS"]+){3}","")}1'
4 5 6

那么您将遇到与所有基于循环的解决方案相同的问题重新分配字段 - FS 转换为 OFS。如果这是一个问题,您需要研究 GNU awks 的 patsplit() 函数。

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

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:

$ echo '1   2 3 4   5    6' |
awk '{sub(/([^ ]+ +){3}/,"")}1'
4   5    6

If you want to accommodate leading spaces and non-blank spaces, but again with the default FS, then it's:

$ echo '  1   2 3 4   5    6' |
awk '{sub(/[[:space:]]*([^[:space:]]+[[:space:]]+){3}/,"")}1'
4   5    6

If you have an FS that's an RE you can't negate in a character set, you can convert it to a single char first (RS is ideal if it'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 "."s separated the fields:

$ echo '1...2.3.4...5....6' |
awk -F'[.]+' '{gsub(FS,RS);sub("([^"RS"]+["RS"]+){3}","");gsub(RS,OFS)}1'
4 5 6

Obviously if OFS is a single char AND it can't appear in the input fields you can reduce that to:

$ echo '1...2.3.4...5....6' |
awk -F'[.]+' '{gsub(FS,OFS); sub("([^"OFS"]+["OFS"]+){3}","")}1'
4 5 6

Then you have the same issue as with all the loop-based solutions that reassign the fields - the FSs are converted to OFSs. If that's an issue, you need to look into GNU awks' patsplit() function.

束缚m 2024-09-04 19:11:18

目前几乎所有答案都添加了前导空格、尾随空格或其他一些分隔符问题。要使用 awk 从第四个字段中选择分隔符为空格且输出分隔符为单个空格的方法是:

awk '{for(i=4;i<=NF;i++)printf "%s",$i (i==NF?ORS:OFS)}' file

要参数化起始字段,您可以执行以下操作:

awk '{for(i=n;i<=NF;i++)printf "%s",$i (i==NF?ORS:OFS)}' n=4 file

以及结束字段:

awk '{for(i=n;i<=m=(m>NF?NF:m);i++)printf "%s",$i (i==m?ORS:OFS)}' n=4 m=10 file

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

awk '{for(i=4;i<=NF;i++)printf "%s",$i (i==NF?ORS:OFS)}' file

To parametrize the starting field you could do:

awk '{for(i=n;i<=NF;i++)printf "%s",$i (i==NF?ORS:OFS)}' n=4 file

And also the ending field:

awk '{for(i=n;i<=m=(m>NF?NF:m);i++)printf "%s",$i (i==m?ORS:OFS)}' n=4 m=10 file
调妓 2024-09-04 19:11:18
awk '{$1=$2=$3="";$0=$0;$1=$1}1'

输入

1 2 3 4 5 6 7

输出

4 5 6 7
awk '{$1=$2=$3="";$0=$0;$1=$1}1'

Input

1 2 3 4 5 6 7

Output

4 5 6 7
感情旳空白 2024-09-04 19:11:18
echo 1 2 3 4 5| awk '{ for (i=3; i<=NF; i++) print $i }'
echo 1 2 3 4 5| awk '{ for (i=3; i<=NF; i++) print $i }'
陈独秀 2024-09-04 19:11:18

避免使用 print 语句的另一种方法:

 $ awk '{$1=$2=$3=""}sub("^"FS"*","")' file

在 awk 中,当条件为 true 时,打印是默认操作。

Another way to avoid using the print statement:

 $ awk '{$1=$2=$3=""}sub("^"FS"*","")' file

In awk when a condition is true print is the default action.

您的好友蓝忘机已上羡 2024-09-04 19:11:18

我不敢相信没有人提供普通的外壳:

while read -r a b c d; do echo "$d"; done < file

I can't believe nobody offered plain shell:

while read -r a b c d; do echo "$d"; done < file
玩套路吗 2024-09-04 19:11:18

选项 1 到 3 存在多个空格的问题(但很简单)。
这就是开发选项 4 和 5 的原因,它们可以毫无问题地处理多个空白。
当然,如果选项 4 或 5 与 n=0 一起使用,则两者都将保留任何前导空格,因为 n=0 意味着不拆分。

选项 1

简单的剪切解决方案(适用于单个分隔符):

$ echo '1 2 3 4 5 6 7 8' | cut -d' ' -f4-
4 5 6 7 8

选项 2

强制 awk 重新计算有时可以解决添加前导空格的问题(适用于 awk 的某些版本):

$ echo '1 2 3 4 5 6 7 8' | awk '{ $1=$2=$3="";$0=$0;} NF=NF'
4 5 6 7 8

选项 3

打印使用 printf< 格式化的每个字段/code> 将提供更多控制:

$ echo '    1    2  3     4   5   6 7     8  ' |
  awk -v n=3 '{ for (i=n+1; i<=NF; i++){printf("%s%s",$i,i==NF?RS:OFS);} }'
4 5 6 7 8

但是,所有先前的答案都将字段之间的所有 FS 更改为 OFS。让我们为此构建几个解决方案。

选项 4

使用 sub 删除字段和分隔符的循环更加可移植,并且不会触发 FS 到 OFS 的更改:

$ echo '    1    2  3     4   5   6 7     8  ' |
awk -v n=3 '{ for(i=1;i<=n;i++) { sub("^["FS"]*[^"FS"]+["FS"]+","",$0);} } 1 '
4   5   6 7     8

注意: “^[”FS”]*” 用于接受输入前导空格。

选项 5

很有可能构建一个不添加额外前导或尾随空格的解决方案,并使用 GNU awk 中的函数 gensub 保留现有空格,如下所示:

$ echo '    1    2  3     4   5   6 7     8  ' |
awk -v n=3 '{ print gensub("["FS"]*([^"FS"]+["FS"]+){"n"}","",1); }'
4   5   6 7     8 

它也可用于交换给定计数 n 的字段列表:

$ echo '    1    2  3     4   5   6 7     8  ' |
  awk -v n=3 '{ a=gensub("["FS"]*([^"FS"]+["FS"]+){"n"}","",1);
                b=gensub("^(.*)("a")","\\1",1);
                print "|"a"|","!"b"!";
               }'
|4   5   6 7     8  | !    1    2  3     !

当然,在这种情况下,OFS 用于分隔行的两个部分,并且字段的尾随空白仍会打印。

注1: ["FS"]* 用于允许输入行中存在前导空格。

Options 1 to 3 have issues with multiple whitespace (but are simple).
That is the reason to develop options 4 and 5, which process multiple white spaces with no problem.
Of course, if options 4 or 5 are used with n=0 both will preserve any leading whitespace as n=0 means no splitting.

Option 1

A simple cut solution (works with single delimiters):

$ echo '1 2 3 4 5 6 7 8' | cut -d' ' -f4-
4 5 6 7 8

Option 2

Forcing an awk re-calc sometimes solve the problem (works with some versions of awk) of added leading spaces:

$ echo '1 2 3 4 5 6 7 8' | awk '{ $1=$2=$3="";$0=$0;} NF=NF'
4 5 6 7 8

Option 3

Printing each field formated with printf will give more control:

$ echo '    1    2  3     4   5   6 7     8  ' |
  awk -v n=3 '{ for (i=n+1; i<=NF; i++){printf("%s%s",$i,i==NF?RS:OFS);} }'
4 5 6 7 8

However, all previous answers change all FS between fields to OFS. Let's build a couple of solutions to that.

Option 4

A loop with sub to remove fields and delimiters is more portable, and doesn't trigger a change of FS to OFS:

$ echo '    1    2  3     4   5   6 7     8  ' |
awk -v n=3 '{ for(i=1;i<=n;i++) { sub("^["FS"]*[^"FS"]+["FS"]+","",$0);} } 1 '
4   5   6 7     8

NOTE: The "^["FS"]*" is to accept an input with leading spaces.

Option 5

It is quite possible to build a solution that does not add extra leading or trailing whitespace, and preserve existing whitespace using the function gensub from GNU awk, as this:

$ echo '    1    2  3     4   5   6 7     8  ' |
awk -v n=3 '{ print gensub("["FS"]*([^"FS"]+["FS"]+){"n"}","",1); }'
4   5   6 7     8 

It also may be used to swap a field list given a count n:

$ echo '    1    2  3     4   5   6 7     8  ' |
  awk -v n=3 '{ a=gensub("["FS"]*([^"FS"]+["FS"]+){"n"}","",1);
                b=gensub("^(.*)("a")","\\1",1);
                print "|"a"|","!"b"!";
               }'
|4   5   6 7     8  | !    1    2  3     !

Of course, in such case, the OFS is used to separate both parts of the line, and the trailing white space of the fields is still printed.

Note1: ["FS"]* is used to allow leading spaces in the input line.

放手` 2024-09-04 19:11:18

Cut 有一个 --complement 标志,可以轻松(快速)地删除列。生成的语法与您想要执行的操作类似 - 使解决方案更易于阅读/理解。补集也适用于您想要删除不连续列的情况。

$ foo='1 2 3 %s 5 6 7'
$ echo "$foo" | cut --complement -d' ' -f1-3
%s 5 6 7
$

Cut has a --complement flag that makes it easy (and fast) to delete columns. The resulting syntax is analogous with what you want to do -- making the solution easier to read/understand. Complement also works for the case where you would like to delete non-contiguous columns.

$ foo='1 2 3 %s 5 6 7'
$ echo "$foo" | cut --complement -d' ' -f1-3
%s 5 6 7
$
甜`诱少女 2024-09-04 19:11:18

不添加前导或尾随空格的 Perl 解决方案:

perl -lane 'splice @F,0,3; print join " ",@F' file

perl @F 自动分割数组从索引 0 开始,而 awk 字段以 $1 开始


Perl 解决方案对于逗号分隔的数据:

perl -F, -lane 'splice @F,0,3; print join ",",@F' file

Python 解决方案:

python -c "import sys;[sys.stdout.write(' '.join(line.split()[3:]) + '\n') for line in sys.stdin]" <文件

Perl solution which does not add leading or trailing whitespace:

perl -lane 'splice @F,0,3; print join " ",@F' file

The perl @F autosplit array starts at index 0 while awk fields start with $1


Perl solution for comma-delimited data:

perl -F, -lane 'splice @F,0,3; print join ",",@F' file

Python solution:

python -c "import sys;[sys.stdout.write(' '.join(line.split()[3:]) + '\n') for line in sys.stdin]" < file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文