awk - read from a file and write conditional output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - read from a file and write conditional output
# 1  
Old 11-20-2014
awk - read from a file and write conditional output

I have a file, which has '|' as separator; I need to read each line from that file and produce output to another file. While reading, I have certain condition on few specific columns (like column3 ='good'); only those lines will be processed.
# 2  
Old 11-20-2014
awk is a good tool to work with individual fields, as in your case.
Please see below:
Code:
 $ cat aa
field 1a| field 2a| good | field 4a
field 1b| field 2b| bad  | field 4b
field 1c| field 2c| good | field 4c

 $ awk -F'|' '{ if($3 ~ "good"){print $0;} }' aa
field 1a| field 2a| good | field 4a
field 1c| field 2c| good | field 4c

If you are not familiar with awk, here are few hints so you can make it work for your specifics. the -F '|' tells awk to use pipe as field delimiter, the $3 is third field, the $0 is whole line

hope it helps.
# 3  
Old 11-20-2014
Thanks. I got the basics. Now here is detailed scenario.
I would like to read each line and then compare multiple columns with ceratin values and then print specific columns to a file or variable.
Here's what I tried to do but got syntax error
Code:
awk '{
if (($3=="3" && $18=="Y") || ($3=="3" && $14=="ELIGIBLE FOR LIFESCAN ONLY"))
    print $1$5$3$6
}' mdm8624CARSd.199065

So, if column3 is 3 and column 18 is Y; OR column3 is 3 and column 14 is FILE; the print columns 1, 5, 3 and 6 to an output file or variable.
The input file do have '|' as field separator.
# 4  
Old 11-21-2014
It's always good to have representative sample input (and output) data, as well as complete error msgs. What error do you get where and when? To me, what you post could be simplified to
Code:
awk     '($3=="3" && ($18=="Y" || $14=="ELIGIBLE FOR LIFESCAN ONLY")) {print $1 $5 $3 $6}
        ' FS="|" mdm8624CARSd.199065

and should not get any syntactical error. Don't forget to assign the field separator!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input write multply output with creteria

Hi All Please Help Read input write multply output with creteria Exemple i have file abc 111 444 abc 111 444 def 111 444 def111 444 bbb 111 444 bbb 111 444 i would need write 3 files pos 1-3 is the Criteria output would be file1 contains abc file2 def file3 bbb ... (3 Replies)
Discussion started by: tonyk334
3 Replies

2. Shell Programming and Scripting

awk conditional output

Hello, How can I use a conditional to produce an output file that varies with respect to the contents of column #4 in the data file: Data file: 9780020080954 9.95 0.49 AS 23.3729 9780020130857 9.95 0.49 AS 23.3729 9780023001406 22.20 0.25 AOD ... (12 Replies)
Discussion started by: palex
12 Replies

3. Shell Programming and Scripting

Read and write in the file

Hello Guys, How all are doing? I have an issue in Unix and want help from all of you I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1' and If at the end of line '1' is written then it... (10 Replies)
Discussion started by: adisky123
10 Replies

4. Shell Programming and Scripting

File Read and Write

I have got a file in following format: AAAAAAA BBBBBBBB CCCCCCC DDDDDDD I am trying to read this file and out put it in following format: AAAAAAA,BBBBBBB,CCCCCCC,DDDDDD Preferred method is shell or Perl. Any help appreciated. (11 Replies)
Discussion started by: Araoki
11 Replies

5. Shell Programming and Scripting

shell, read table and write the value for each factor to output

Hey guyz, I have a table like this: 1 A=#;B=#;C=# 2 A=#;C=#;D=#;E=#;E=# 3 B=#;B=#;B=#;D=# # are just some numbers. I want to have the output like this: * 1 2 3 A # # NA B # NA # C # # NA D NA # # E NA # NA So basically, I wanna know in each of the rows in my input (which... (9 Replies)
Discussion started by: @man
9 Replies

6. UNIX for Dummies Questions & Answers

Read / write file exemples

hello world, i was looking for exemples for writing ans reading in / from a file, more exactly a text file; and how i'm only at very beagining, if anyone have some exemples very simple, very 'classic' , -with explications- and not hard to undersand . i was wondering that some of you are theacher... (6 Replies)
Discussion started by: unumai
6 Replies

7. Shell Programming and Scripting

Write Awk output to a file , inside script.

Hi, Can anyone please help me with this issue. I have a Awk command which take file as input, and provides the output having multiple lines, its working in command mode, but not if i plug it in script. #!/bin/ksh infile=a.txt outfile=b.txt awk ' BEGIN{ FS=OFS="|";ORS = "\n";... (1 Reply)
Discussion started by: sp999
1 Replies

8. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

9. Shell Programming and Scripting

Read/write file with scripting

Is there any way to write to a text file with scripting? I need to write to a text file two lines of text for the amount of files in the current directory. (9 Replies)
Discussion started by: Fred Goldman
9 Replies

10. Shell Programming and Scripting

read and write from a file

I have tried to show the file name whose size is greater than 200 byte in current directory. Please help me. ls -l | tr -s " " " " | cut -f 5,9 -d " " >out.txt #set -a x `cat out.txt` i=0 `cat out.txt` | while do read x echo $x #re=200 j=0 if }" < "200" ] then echo $j j=`expr $j... (2 Replies)
Discussion started by: rinku
2 Replies
Login or Register to Ask a Question