Error passing parameter in "sub" command in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error passing parameter in "sub" command in awk
# 1  
Old 03-05-2012
Data Error passing parameter in "sub" command in awk

I have to replace the pattern found in one file in another file with null/empty "" if found on the fields 3 or 4 ONLY

Code:
File 1
====
10604747|Mxdef|9999|9999|9999|2012-03-04 00:00:59
10604747|Mcdef|8888|9999|8888|2012-03-04 00:00:59
.
.
.
File 2
====
9999
8888
.
.
.
 
Expected output:
 
File 1
====
10604747|Mxdef|||9999|2012-03-04 00:00:59
10604747|Mcdef|||8888|2012-03-04 00:00:59

I tried something like

while read i
do
awk -v FS="|" -v OFS="|" 'BEGIN { split("3|4", F); } { for(N in F) sub(/'$i'/, "", $(F[N])) } 1' File1 > File1_tp
mv File1_tp File1
done < File2

This is not working. I am getting error in the awk statement inside the while loop. The parameter passing is not working fine.Smilie

Someone, please help on this.Smilie
# 2  
Old 03-05-2012
Code:
awk -F"|" -v OFS="|" 'NR==FNR{a[$0];next}$3 in a{$3=""}$4 in a{$4=""}1' file2 file1

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-05-2012
Try:
Code:
awk 'NR==FNR{A[$1];next}$3 in A{$3=x}$4 in A{$4=x}1' file2 FS=\| OFS=\| file1

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-05-2012
THanks both the codes work. But if number of date fields increases, the code becomes more lengthy...

---------- Post updated at 09:25 PM ---------- Previous update was at 09:24 PM ----------

Can some one please explain one of the code?

---------- Post updated at 10:20 PM ---------- Previous update was at 09:25 PM ----------

How to make the code dynamic so that it can handle multiple fields rather than restricting it to two fields?
# 5  
Old 03-05-2012
You agreed not to bump posts when you registered. Please stop doing it. We are not "on call"; if someone doesn't answer your post immediately, wait!

What exactly do you want? Do you need to give it a list of fields, a range of fields, or what?

---------- Post updated at 11:05 AM ---------- Previous update was at 10:54 AM ----------

For a list of fields separated by |:

Code:
awk -v FIELDS="3|4" -v FS="|" -v OFS="|" 'BEGIN { split(FIELDS, F); }
NR==FNR { A[$1]=1; print "skip " $1; next }
{ for(X in F) if(A[$(F[X])]) $(F[X])="" } 1' fields data
10604747|Mxdef|||9999|2012-03-04 00:00:59
10604747|Mcdef|||8888|2012-03-04 00:00:59

$

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 03-05-2012
I dint mean to bump the post. That was totally unintentional Smilie
# 7  
Old 03-05-2012
If you edit, that doesn't bump.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

2. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Help with error "por: 0403-012 A test command parameter is not valid."

Hi, im asking for help with the next script: echo ^; then if then printf "\033 query1.sh: export TERM=vt100 export ORACLE_TERM=at386 export ORACLE_HOME=/home_oracle8i/app/oracle/product/8.1.7 export ORACLE_BASE=/home_oracle8i/app/oracle export... (8 Replies)
Discussion started by: blacksteel1988
8 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

passing a list of dynamic names to a "PS" command in shell script?

Hi, I am new to shell script. This is my first post .I have written a small script which returns list of names starts with "ram" in /etc/passwd .Here is that:- #!/bin/ksh NAME_LIST="name_list.txt" cat /dev/null > $NAME_LIST evalcmd="cat /etc/passwd | grep "^ram?*" | cut -d: -f1" eval... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

10. Shell Programming and Scripting

How to get Find command work with a variable passing "*" value?

Hi guy, I have a problem to pass a variable containing '*' value to FIND command. below is the script. It doesn't work by submit below command: rmf.sh name '*.txt' or rmf.sh name *.txt I've tried either optn="-name '$2'" or optn="-name $2"., and there is no luck. ### (script... (5 Replies)
Discussion started by: unxuser
5 Replies
Login or Register to Ask a Question