Unable to match string within awk printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to match string within awk printf
# 1  
Old 07-11-2013
Unable to match string within awk printf

Hi All

I am working to process txt file into csv commo separated.
Code:
Input.txt
1,2,asdf,34sdsd,120,haahha2
2,2,wewedf,45sdsd,130,haahha
.....
....
 
Errorcode.txt
120
130
140
 
myawk.awk code:
 
{
BEGIN{
HEADER="f1,f2,f3,f4,f5,f6"
print HEADER}
printf '%s,%s,%s,%s,%s,%s\n",substring(..),....
}
awk -f myawk.awk Input.txt

This is fine.
Now I need to add one more header in csv as status. If field5 matches the error code in Errorcode.txt then I say "Failed" else passed.
I need to do this in printf only as I want code should be sort. I tried using match function but did not work( I basically want to perform evaluation true of false in printf by matching feld value from other file.

Can some one help me to do this?

Thanks
Krsna
# 2  
Old 07-11-2013
Code:
echo "Hello World!" | awk '{printf "%s", substr($1,0,2);}'

gave me He as output
# 3  
Old 07-11-2013
Quote:
Originally Posted by PikK45
Code:
echo "Hello World!" | awk '{printf "%s", substr($1,0,2);}'

gave me He as output
Surprises me. For me, it prints H.

For the original request, try:
Code:
awk     'BEGIN          {print "f1,f2,f3,f4,f5,f6,Status"}
         NR==FNR        {Err[$1]++;next}
                        {print $0, ($5 in Err)?"Failed":"Passed"}
        ' FS="," OFS="," file1 file
f1,f2,f3,f4,f5,f6,Status
1,2,asdf,34sdsd,120,haahha2,Failed
2,2,wewedf,45sdsd,130,haahha,Failed

# 4  
Old 07-12-2013
In this case I would define FS and OFS in the BEGIN section, rather than requiring two extra parameters.
Code:
BEGIN          {FS=OFS=","; print "f1,f2,f3,f4,f5,f6,Status"}

# 5  
Old 07-17-2013
Unable to match string within awk printf

Hi RudhiThanks a lot for your solution...I was thinking in same lines but could not know how to do exaclty. I built my logic on this. Hi MadeInGermanyThanks you too.Guys very sorry for replying very late...ThanksKrsna
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

2. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

3. UNIX for Dummies Questions & Answers

awk for trimming a string up to the first, then second, then third... match

Hi ! With awk, I would need to trim a string from the beginning up to the first occurrence of "1", then from the beginning up to the second occurrence of "1", then from the beginning up to the third, then the fourth...., then the last occurrence of "1". input: 1aaa1bb1ccccccc dd1e1ffff... (7 Replies)
Discussion started by: beca123456
7 Replies

4. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

5. Shell Programming and Scripting

How to get part of string in awk from match

Hi, Im an awk noob and I am having trouble trying to get matches. Here is my script: #!/bin/gawk -f BEGIN {} $0 ~ /<a href=".*">.*<\/a>/{print} Ideally I want to be able to get the actual link and print it. In PHP you can do preg_replace and get the match you want by using \\1 where 1... (2 Replies)
Discussion started by: adsyuk
2 Replies

6. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

7. Shell Programming and Scripting

Explanation for printf string in awk

hi all can any one help me to understand this bdf -t vfxs | awk '/\//{printf("%-30s%-10s%-10s%-10s%-5s%-10s\n",$1,$2,$3,$4,$5,$6)}' i want to understand the numbers %-30S% (4 Replies)
Discussion started by: maxim42
4 Replies

8. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

9. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

10. UNIX for Advanced & Expert Users

how can awk match multi pattern in a string

Hi all, I need to category the processes in my system with awk. And for now, there are several command with similar name, so i have to match more than one pattern to pick it out. for instance: binrundb the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"... (5 Replies)
Discussion started by: sleepy_11
5 Replies
Login or Register to Ask a Question