nawk script to manipulate txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nawk script to manipulate txt file
# 1  
Old 10-02-2012
nawk script to manipulate txt file

Hi experts, I am not familiarised using nawk/awk to manipulate txt file. So I need ur help here. thanks.
txt1 - qc_m.log
Code:
 
TQC101
TQC102
TQC103
TQC104

txt2 - resultEqpt.log
Code:
TQC102,,LOGOFF
TQC103,,LOGOFF
TQC104,2012-10-03 07:42:57,LOGON

so TQC101 is not found in txt 2.

result
Code:
TQC101,,LOGOFF
TQC102,,LOGOFF
TQC103,,LOGOFF
TQC104,2012-10-03 07:42:57,LOGON


Last edited by ment0smintz; 10-03-2012 at 12:39 AM..
# 2  
Old 10-02-2012
Are both input files sorted?

Do you want ",,LOGOFF" for any string not found in txt2?
# 3  
Old 10-02-2012
Quote:
Originally Posted by Chubler_XL
Are both input files sorted?

Do you want ",,LOGOFF" for any string not found in txt2?
yes both are sorted

yes that's the idea

I tried

Code:
 
nawk -F, 'FNR==NR{a[$1]=$2;next}{if($1 in a) $??? ;else $(NF+1)=",,LOGOFF"}1' OFS=,

Dont know what to add in
# 4  
Old 10-02-2012
try:
Code:
# solaris use nawk not awk
awk -F'[ ,]'   ' FILENAME=="txt1" {arr[$1]++}
                FILENAME=="txt2" {arr[$1]++; print $0}
                END{ for (i in arr) {if(arr[i]==1){printf("%s%s\n", i,", ,LOGOFF" )  } } } ' txt1 txt2  > newfile

start with that to see if it is what you want.

Last edited by jim mcnamara; 10-03-2012 at 12:10 AM..
# 5  
Old 10-03-2012
Quote:
Originally Posted by jim mcnamara
try:
Code:
# solaris use nawk not awk
awk -F'[ ,]'   ' FILENAME=="txt1" {arr[$1]++}
                FILENAME=="txt2" {arr[$1]++; print $0}
                END{ for (i in arr) {if(arr[i]==1){print arr[i],", ,LOGOFF"  } } } ' txt1 txt2  > newfile

start with that to see if it is what you want.
result:
Code:
TQC101
TQC102
TQC103
TQC104
1 , ,LOGOFF
1 , ,LOGOFF
1 , ,LOGOFF
1 , ,LOGOFF

# 6  
Old 10-03-2012
It's late, and ....ooops. See the red change in my post above. My apologies.
# 7  
Old 10-03-2012
How about:

Code:
join -a 1 -t, -o 1.2 2.2 2.3 txt1 txt2

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

2. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

Nawk script to compare records of a file based on a particular column.

Hi Gurus, I am struggling with nawk command where i am processing a file based on columns. Here is the sample data file. UM113570248|24-AUG-11|4|man1|RR211 Alert: Master Process failure |24-AUG-11 UM113570624|24-AUG-11|4|man1| Alert: Pattern 'E_DCLeDAOException' found |24-AUG-11... (7 Replies)
Discussion started by: usha rao
7 Replies

7. Shell Programming and Scripting

Shell script to manipulate a file

Hello, I have a file with following contents : WSL SRVGRP=LISTENER SRVID=2 CLOPT="-A -t -- -n 0x0002aa050a03cc65 " RQPERM=0660 REPLYQ=Y RPPERM=0660 MIN=1 MAX=1 CONV=N I need to print only the value in Hex i.e.... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

8. Shell Programming and Scripting

sed script to manipulate the /etc/passwd file

Hi. Can anybody help me with a script to extract usernames, shell and home_directory from the /etc/passwd file. I am stuck (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. Shell Programming and Scripting

unix script to takes the old data from a TXT file and compress them into new file

Hi, I am looking for the unix script which can takes the 2 month old data from a TXT file (there is one txt file in whiche messages are appended on daily basis) and compress them into new file.Please halp me out. (2 Replies)
Discussion started by: vpandey
2 Replies
Login or Register to Ask a Question