Script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script help
# 15  
Old 03-02-2005
Quote:
Originally Posted by penfold
is there anyway to overcome this as the only field common to both is the second field. would I have to create a variable out of the second fields in core.txt and then from that check gmrd.txt
ok, how about if you give a desired output based on the sample input files you provided.
# 16  
Old 03-02-2005
gmrd.txt:

;;BAY GR;03;1344;432
;;BAY GR;04;4321;221
;;235649;03;2563;598
;;235649;03;2563;345
;;291802;00;2563;598
;;979217;00;2563;598
;;235649;03;2563;598
;;A0ABVB;00;2563;598
;;235649;03;2563;598

core.txt

;;BAY GR;00;123
;;NOASCOMGR;03;143
;;US36204YA782;00;156
;;A0ABVB;00;132


The desired output would look at field three of core.txt and compare that with field three of gmrd.txt and only print those lines where gmrd.txt has the same field three value as core.txt so the output would be:

output.txt:
;;BAY GR;03;1344;432
;;BAY GR;04;4321;221
;;A0ABVB;00;2563;598
# 17  
Old 03-02-2005
Code:
awk '
  BEGIN {
  FS=OFS=";"
  }

  FNR==NR { arr[$3]= ($3 in arr) ? arr[$3] "\n" $0 : $0; next }
  $3 in arr { print arr[$3] }' gmrd.txt core.txt


Last edited by vgersh99; 03-02-2005 at 12:50 PM..
# 18  
Old 03-02-2005
this comes out with the following output:

./temp4.sh[8]: awk^JBEGIN {^JFS=OFS=";"^J}^J^JFNR==NR { arr[$3]= ($3 in arr) ? a
rr[$3] "\n" $0 : $0; next }^J$3 in arr { print arr[$3] }^Jgmrd.txt: not found

- could this be to do with the flavour of UNIX im using? (AIX)
# 19  
Old 03-02-2005
Quote:
Originally Posted by penfold
this comes out with the following output:

./temp4.sh[8]: awk^JBEGIN {^JFS=OFS=";"^J}^J^JFNR==NR { arr[$3]= ($3 in arr) ? a
rr[$3] "\n" $0 : $0; next }^J$3 in arr { print arr[$3] }^Jgmrd.txt: not found

- could this be to do with the flavour of UNIX im using? (AIX)
I've only posted a relative AWK portion - the invokation sequence stayed the same.

I've reposted the entire "thing" now.
# 20  
Old 03-02-2005
Code:
awk -F\; 'FNR==NR{arr[$3]=1;next};$3 in arr' core.txt gmrd.txt

;;BAY GR;03;1344;432
;;BAY GR;04;4321;221
;;A0ABVB;00;2563;598
# 21  
Old 03-22-2005
Can anyon help with this?

Is anyone able to explain what vgersh99 has done above? I'm new to scripting and am finding it immensly difficult trying to figure out what the following statement does exactly:

awk '
BEGIN {
FS=OFS=";"
}

FNR==NR { arr[$3]= ($3 in arr) ? arr[$3] "\n" $0 : $0; next }
$3 in arr { print arr[$3] }' gmrd.txt core.txt


Particular points that I dont understand are:

Why do we set FNR==NR?
Also what is the question mark for after that statement arr[$3]= ($3 in arr) ?

What does the : do after the $0

Thanks in advance for any help
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question