删除列awk

时间:2017-03-07 07:23:41

标签: awk split gsub substr

我有输入文件:

HEADER 1     |  HEADER 2   |  HEADER 3      |  HEADER 3   | HEADER 4       |
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|

我希望我的文件类似:(包含3个带标题和预告片的拆分文件以及删除前3列

DETAIL 07032017
 HEADER 1    |  HEADER 2      |       
1356438284972|1356438292151693|
EOF 3

DETAIL 07032017
 HEADER 1    |  HEADER 2      |
1356438284972|1356438292151693|
EOF 3

DETAIL 07032017
 HEADER 1    |  HEADER 2      |
1356438284972|1356438292151693|
EOF 3

至于我目前的命令;

awk -v date="$(date +"%d%m%Y")" -F\| 'NR==1 {h=$0; next} 
{file="FILE_"$1"_"$2"_"date".csv";  
print (a[file]++?"": "DM9 "date"" ORS h ORS) $0> file} END{for(file in a)     
print "EOF " a[file] > file}' testing.csv

我只能得到(如上面的命令)我可以分割文件,添加标题和预告片,但尚未删除列

DETAIL 07032017
 HEADER 1    |  HEADER 2   |  HEADER 3      |  HEADER 3   | HEADER 4       |
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693| 
EOF 3

DETAIL 07032017
 HEADER 1    |  HEADER 2   |  HEADER 3      |  HEADER 3   | HEADER 4       |
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693| 
EOF 3

DETAIL 07032017
 HEADER 1    |  HEADER 2   |  HEADER 3      |  HEADER 3   | HEADER 4       |
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|
EOF 3

以上命令仅用于拆分,添加带有记录计数的标题和预告片。

但我希望在拆分后删除前3列并添加标头预告片。有可能吗?

或者我需要拆分然后删除只有最后一个做标题和预告片?

我试试这个;

awk -v date="$(date +"%d%m%Y")" -F\| 'NR==1 {h=$0; next} 
{file="FILE_"$1"_"$2"_"date".csv";  
print (a[file]++?"": "DM9 "date"" ORS h ORS)**substr($0, index($0,$4))**>             file} END{for(file in a) print "EOF " a[file] > file}' testing.csv

我使用了substr但根本没用。

我想删除第一个3栏

3 个答案:

答案 0 :(得分:2)

正如我在上一篇文章中所写,您可以使用substr($0, index($0,$4))删除第1列到第3列。 上面的示例文件缺少标题,因此我添加了一个标题来进行测试。

下面的测试结果应该能满足您的期望。

如果你的机器不起作用,可能是你的电脑有所不同。

echo "cat data.csv"
cat data.csv
echo "awk - started "
awk -v date="$(date +"%d%m%Y")" -F"|" 'NR==1 {h=substr($0, index($0,$4)); next} \
{file="FILE_"$1"_"$2"_"date".csv";print (a[file]++?"": "DM9 "date"" ORS h ORS) substr($0, index($0,$4)) >file } \
END{for(file in a) print "EOF " a[file] >file}' data.csv
echo "awk - finished "
echo "csv file generated:"
cat FILE*.csv
echo "script finish"

#Terminal Output:
cat data.csv
Header1|Header2|Header3|Header4|Header5|
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693| 
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|
1356438283301|1356438284971|1356438292151697|1356438284972|1356438292151693|
awk - started
awk - finished
csv file generated:
DM9 07032017
Header4|Header5|
1356438284972|1356438292151693|
1356438284972|1356438292151693|
1356438284972|1356438292151693|
EOF 3
script finish 

答案 1 :(得分:1)

试试这个 -

    awk -v date="$(date +"%d%m%Y")" -F\| 'BEGIN{print "DETAIL " date} {print $4,$5FS} END {print "EOF " NR}' OFS="|"  f
DETAIL 07032017
1356438284972|1356438292151693|
1356438284972|1356438292151693|
1356438284972|1356438292151693|
EOF 3

答案 2 :(得分:0)

awk -v Date="$(date +'%d%m%Y')" '
   # print header
   BEGIN { print "DETAIL " Date }
   # prepare new content
      { sub( /([^|][|]){3}/, "") }
   # print new content
   7
   # print footer
   END { print "\nEOF " NR }
   # redirect output to your final file
   ' YourSource > YourFile