awk command not replacing in first line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk command not replacing in first line
# 1  
Old 09-24-2015
awk command not replacing in first line

As per requirement if column 2 is NULL then 'N' ELSE 'Y'.

I have written below awk code. But it is not replacing values for first line. Smilie

Code:
cat temp.txt  
1|abc|3  
1||4  
1|11|c

Code:
 awk -F'|' '{if($2==""){$2="N"}else{$2="Y"} print $0 } {OFS="|"} '  <  temp.txt  

1 Y 3  
1|N|4  
1|Y|c

# 2  
Old 09-24-2015
Hello max_hammer,

yes it is expected only as you have given print $0 first and then {OFS="|"} so when the line was printed at that time awk will take default value of OFS which is space only. Try setting it at BEGIN{OFS="|"}, it should work then.

EDIT: You can use following also, hope this is helpful to you.
Code:
 awk -F'|' '{if($2==""){$2="N"} else{$2="Y"}};{ print $0 }' OFS="|" temp


Thanks,
R. Singh
# 3  
Old 09-24-2015
Doesn't it? abc is NOT "", so a "Y" is correctly printed. You might want to define OFS upfront...
# 4  
Old 09-24-2015
Code:
$ cat max_hammer.file
1|abc|3
1||4
1|11|c

awk using the ternary operator which can substitute a simple if-else clause.
awk using just a true value 1 which triggers the default action print $0
Code:
$ awk -F"|" '{$2=="" ? $2="N" : $2="Y"}1' OFS="|" max_hammer.file
1|Y|3
1|N|4
1|Y|c

# 5  
Old 09-25-2015
The ternary operator option could be reduced a bit further still:
Code:
awk '{$2=($2=="" ? "N" : "Y")}1' FS=\| OFS=\| file

The parentheses are not necessary, but they help for clarity.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

4. Shell Programming and Scripting

Replacing last line with awk and change the file name

Hi Guys, I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file. for egf1_20140101.txt aa|aus|20140101|yy bb|nz|20140101|yy . .... (19 Replies)
Discussion started by: rohit_shinez
19 Replies

5. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

6. Shell Programming and Scripting

awk for replacing line feed

Hello all, I have data like "1"|"My_name"|"My_Email"|"My_Last"|My_other" "2"|"My_name"|"My_Email"|"My_Last"|My_other" "3"|"My_name"|"My_Email"|" "|My_other" "1"|"My_name"|"My_Email"|"My_Last"|My_other" Need output like "1"|"My_name"|"My_Email"|"My_Last"|My_other"... (10 Replies)
Discussion started by: lokaish23
10 Replies

7. Shell Programming and Scripting

Replacing text in Perl given by command line

Hi I need to write a Perl script that the file given as first argument of the command line that will find all occurrences of the string given as the third argument of the command line and replace with the string given as the fourth argument. Name newfound file is specified as the second... (3 Replies)
Discussion started by: nekoj
3 Replies

8. Shell Programming and Scripting

refine awk command in replacing carriage return

Hi, need your help in below,I have 4 types of file need to be processed so that it will replace carriage return in Remarks column with <:::> Remarks column position may varies in different types of file. sample file: col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10... (8 Replies)
Discussion started by: agathaeleanor
8 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. Shell Programming and Scripting

replacing a string in a file with command line parameter

Hello, I am trying to replace a string with a paramter given along with the script. I am replacing application1 to application2 with the script: ./change_app.sh application2 change_app.sh: #!/bin/ksh grep $1 applications.dat 2>&1 >/dev/null echo $1 file=pckage.new sed 's/Name:... (5 Replies)
Discussion started by: chiru_h
5 Replies
Login or Register to Ask a Question