简体   繁体   English

使用 AWK 替换列值

[英]Replace a column value using AWK

I have a file named as source-data.lst (created using ls -l) which contains the following information:我有一个名为source-data.lst的文件(使用 ls -l 创建),其中包含以下信息:

-rwxrwxrwx 1 soumyadipd soumyadipd 379 Apr  2 17:07 filegen.awk
-rw-rw-r-- 1 soumyadipd soumyadipd 129 Mar 31 13:35 file_name_list.txt
-rw-rw-r-- 1 soumyadipd soumyadipd 624 Apr  2 16:32 log
-rw-r--r-- 1 soumyadipd root         0 Apr  3 12:38 source-data.lst
-rw-rw-r-- 1 soumyadipd soumyadipd 676 Apr  2 16:32 temp
-rw-rw-r-- 1 soumyadipd soumyadipd 157 Mar 31 15:10 Type1_LP_OUT.txt

I need to update the file size by 1 for every row.我需要将每一行的文件大小更新 1。

by executing awk '{ $5=$5+1; print $0 }' source-data.lst通过执行awk '{ $5=$5+1; print $0 }' source-data.lst awk '{ $5=$5+1; print $0 }' source-data.lst from terminal the output will be as follows:从终端awk '{ $5=$5+1; print $0 }' source-data.lst输出如下:

[soumyadipd@linuxpc awkscripts]$ awk '{ $5=$5+1; print $0 }' source-data.lst
-rwxrwxrwx 1 soumyadipd soumyadipd 380 Apr 2 17:07 filegen.awk
-rw-rw-r-- 1 soumyadipd soumyadipd 130 Mar 31 13:35 file_name_list.txt
-rw-rw-r-- 1 soumyadipd soumyadipd 625 Apr 2 16:32 log
-rw-r--r-- 1 soumyadipd root 1 Apr 3 12:38 source-data.lst
-rw-rw-r-- 1 soumyadipd soumyadipd 677 Apr 2 16:32 temp
-rw-rw-r-- 1 soumyadipd soumyadipd 158 Mar 31 15:10 Type1_LP_OUT.txt
[soumyadipd@linuxpc awkscripts]$

But the problem is that I have an AWK script named as filegen.awk as:但问题是我有一个名为filegen.awk的 AWK 脚本:

BEGIN{
      noOfSourceData=0;
    while ((getline data < "source-data.lst") > 0) {
        noOfSourceData++;
        sourceRecordsList[noOfSourceData] = data;
    }
}{

}END{
for (sd=1; sd<= noOfSourceData; sd++) {
#print sourceRecordsList[sd]
print sourceRecordsList[sd] > "temp"
awk '{ $5=$5+1; print $0 }' temp
}
}

Using this filegen.awk I have to increase the file size of source-data.lst .使用这个filegen.awk我必须增加source-data.lst的文件大小。 So executing the script as follows I found the following error:所以按如下执行脚本我发现了以下错误:

[soumyadipd@linuxpc awkscripts]$ awk -f filegen.awk source-data.lst > log
awk: filegen.awk:16: awk '{ $5=$5+1; print $0 }' temp
awk: filegen.awk:16:     ^ invalid char ''' in expression
[soumyadipd@linuxpc awkscripts]$

Kindly help me to make a solution... thanks in advance.请帮助我制定解决方案......提前致谢。

Instead of using getline into a variable getline data < "source-data.lst" you could use just getline from a file:您可以只使用文件中的getline ,而不是将getline用于变量getline data < "source-data.lst"

awk '
BEGIN{
    noOfSourceData=0;
    while ((getline < "source-data.lst") > 0) {
        noOfSourceData++;
        $5=$5+1;
        sourceRecordsList[noOfSourceData] = $0;
    }
}
{

}

END{
    for (sd=1; sd<= noOfSourceData; sd++)
        print sourceRecordsList[sd]
}' dummy.txt

I solved my problem, and the code of filegen.awk is as follows:我的问题解决了, filegen.awk的代码如下:

BEGIN{
    noOfSourceData=0;
    while ((getline data < "source-data.lst") > 0) {
        noOfSourceData++;
        sourceRecordsList[noOfSourceData] = data;
    }
}{

}END{
for (sd=1; sd<= noOfSourceData; sd++) {
system("echo \"" sourceRecordsList[sd] "\" | awk '{ $5=$5+1; print $0 }'");
}
}

And I get little idea from here while searching for the solution.在寻找解决方案时,我从这里得到的想法很少。

Thanks everybody for replying.谢谢大家的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM