简体   繁体   中英

Replace a column value using AWK

I have a file named as source-data.lst (created using ls -l) which contains the following information:

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

by executing 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:

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

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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