awk if else within 1 line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if else within 1 line
# 1  
Old 10-05-2016
awk if else within 1 line

Hello,
I get data from an input file and write it to an output like this:
Code:
cat CATA.csv|sed 's/"//g'|egrep '|XHA|CCD|DDD'|awk '{print "E164",$5$8$11,$5$8$11,"12/01/2015","12/01/2099","A",$14,$27,$2"_"$27,date,date,"gxxxxxx","0"}' FS='|' OFS='|' > test.txt

Problem is, that sometimes $27 is empty.
And when it is empty, I'd like to populate it with some default_value.
How can I do this within the same line?

Thanks!

BR,
Atanas
# 2  
Old 10-05-2016
Hello apenkov,

As you haven't showed us the sample Input_file so it was not possible to test it, could you please try following and let me know if this helps you.
Code:
 cat CATA.csv|sed 's/"//g'|egrep '|XHA|CCD|DDD'|awk '{$27=$27?$27:"default_value";print "E164",$5$8$11,$5$8$11,"12/01/2015","12/01/2099","A",$14,$27,$2"_"$27,date,date,"gxxxxxx","0"}' FS='|' OFS='|' > test.txt

Though your above could be reduced to single awk by doing some fine tuning eg--> cat,sed,egrep parts could be included into a single awkas awkcould read the Input_file by itself(cat CATA.csvpart) and could do substitution(sedpart) and could search too(egreppart). But without test Input_file, we could only tell.

Thanks,
R. Singh
# 3  
Old 10-05-2016
Try (untested as input data were NOT posted):
Code:
awk '/XHA|CCD|DDD/ {gsub (/"/, _); $27=$27?$27:"default_value"; print "E164",$5$8$11,$5$8$11,"12/01/2015","12/01/2099","A",$14,$27,$2"_"$27,date,date,"gxxxxxx","0"}' FS='|' OFS='|' CATA.CSV

# 4  
Old 10-05-2016
Hi,
Thanks for prompt replies.
When I add that part:
Code:
$27=$27?$27:"default_value";

I get an error:
Code:
awk: syntax error near line 1
awk: illegal statement near line 1

Input below:
Code:
"Afg"|"AFG"|"AF"|"00"|"93"|""|""|""|""|""|""|"7"|""|"DDD"|""|""|""|"2015-01-13 00:00:00"|""|""|"412"|""|""|""|""|""|"                                 "|""|""|""|""|""|"2009-01-31 00:00:00"
"Afg"|"AFG"|"AF"|"00"|"93"|"0"|"N"|"20"|""|""|""|"7"|"Kab"|"CCD"|""|""|""|"2015-01-13 00:00:00"|""|""|"412"|""|""|""|""|""                                 |""|""|""|""|""|""|"2004-11-05 00:00:00"

Thanks!
# 5  
Old 10-05-2016
Hello apenkov,

As you haven't told us your O.S which you are using, so if you are on a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk. Because when I tried Rudi's code or mine code they both work fine and successfully for me.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 10-05-2016
What's your OS, shell, and awk version?


Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-05-2016
Ohhhh.....always forget that part
Code:
/usr/xpg4/bin/awk

It works. Thanks a lot guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

3. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

4. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

5. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

6. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

7. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

10. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question