OFS does not apply to few records in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting OFS does not apply to few records in awk
# 1  
Old 04-28-2013
OFS does not apply to few records in awk

Hi ,

I am having a problem with my awk oneliner , which for some reason leaves the first two records

Input File
Code:
$ cat file1
A1:B1:C1:NoLimit
M1:M2:M3:Limit
A2:B2:C2,C3,C4,C5
A3:B3:C3,C4,C5,C6,C7

Desired output
Code:
A1,B1,C1,NoLimit
M1,M2,M3,Limit
A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7

I am using the below oneliner to achieve it partially but i dont get why the first two records OFS does not change to comma ,
Code:
nawk 'BEGIN{FS=":";OFS=","}{gsub(",","\n,,,",$NF);print}' file1

This gives me an output
Code:
A1:B1:C1:NoLimit
M1:M2:M3:Limit
A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7

Can someone please help me understand the problem and correct the code
# 2  
Old 04-28-2013
Try: ..);$1=$1;print

In the first two lines there are no commas in the last field, so no substitution takes place and the field does not get modified and therefore the record does not get recalculated and so FS does not get replaced by OFS.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-28-2013
It Worked!!

Quote:
Originally Posted by Scrutinizer
Try:
..);$1=$1;print
Wow!!! , It worked , Can you please explain the logic behind it ?
# 4  
Old 04-28-2013
I had added a bit of explanation to my post. By adding $1=$1 (assigning $1 onto itself) you force the record to be recalculated..
# 5  
Old 04-28-2013
Thanks i got your explanation , but i am confused with awk basics,

Code:
nawk 'BEGIN{FS=":";OFS=","}{print}' file1

Should not awk make the changes of the : to , , but i see that didnt happen. So does that mean awk will update the OFS only when it sees some change done to a record ?
# 6  
Old 04-28-2013
It doesn't alter the input line at all unless you do something to one of the tokens.

If that's all you want, awk is overkill anyway, try tr ':' ',' < inputfile > outputfile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - OFS printing duplicate. Why?

Why the following code printing duplicate records? bash-4.1$ cat rm1 c1 c2 c3 l1 2 3 4 l2 2 3 2 bash-4.1$ awk '{print $0} OFS = "\n"' rm1 c1 c2 c3 c1 c2 c3 l1 2 3 4 l1 2 3... (4 Replies)
Discussion started by: quincyjones
4 Replies

2. Shell Programming and Scripting

OFS print awk

file: sasa|asasa|asasa|asas erer|Erer|rere|ererer Output needed : sasa:asasa:asasa:asas erer:Erer:rere:ererer Im getting output, when i use the $1,$2. awk -F'|' 'BEGIN{OFS=":";} {print $1,$2; }' file Output : sasa:asasa erer:Erer But when i need the whole column, i... (5 Replies)
Discussion started by: Ramesh M
5 Replies

3. UNIX for Dummies Questions & Answers

OFS in awk

Hello, I have an issue with adding commas as delimiters in this scenario: cat xtr3.rpl|head -5|awk 'BEGIN {OFS=","} {print $1,$2,$3,$4}' Produces this output: 00530083,0000000471,000000000000.00,000000000000.00 00530085,0000000471,000000000000.00,000000000000.00... (10 Replies)
Discussion started by: MIA651
10 Replies

4. Shell Programming and Scripting

Awk OFS issues

Hi, Could anyone tell me what Im doing wrong here any help will be much appreciated #!/bin/bash ls -ltr /export/home/tjmoore > /export/home/tjmoore/log100 awk -F " " /export/home/tjmoore/log100 'BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5, $6,$7,$8,$9;}' > /export/home/tjmoore/log1001 I... (9 Replies)
Discussion started by: 02JayJay02
9 Replies

5. Shell Programming and Scripting

Awk OFS issues

Hi Im trying to tidy up the output of a who command when it writes to a log, everything I've tried doesnt seem to work though, any help would be massively appreciated. Im using the awk command to set the OFS as tab. #!/bin/bash who >> /export/home/tjmoore/logusers awk -F 'BEGIN... (3 Replies)
Discussion started by: 02JayJay02
3 Replies

6. UNIX for Dummies Questions & Answers

Problem with AWK and OFS

I have a file that looks like this: Infile.seq I want to output the DNA sequence and add the filename as the identifier. The output file should look like this: I am using the following code but I do not understand why the sequence is not in the output: awk 'BEGIN { RS =... (11 Replies)
Discussion started by: Xterra
11 Replies

7. Shell Programming and Scripting

Apply condition on fixed width file and filter records

Dear members.. I have a fixed width file. Requirement is as below:- 1. Scan each record from this fixed width file 2. Check for value under field no "6" equals to "ABC". If yes, then filter this record into the output file Please suggest a unix command to achieve this, my guess awk might... (6 Replies)
Discussion started by: sureshg_sampat
6 Replies

8. Shell Programming and Scripting

AWK - OFS

Hi All, I have a comma seperated delimited file with 10 columns. I need to convert it into TAB seperated delimited file. awk -F"," '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10}' a.txt >> b.txt how to use OFS to get the same output. I have tried by googling, but it... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

9. Shell Programming and Scripting

OFS in awk.

OFS is inbuild command in awk. I have a file file.txt abc : def : ghi jkl : mno: pqr stu : vwx :yzz code i used: awk -F ":" 'BEGIN {OFS="|"} {print $1,$2}' file.txt output: abc def jkl mno stu vwx but as i have used OFS="|" and i am expecting output as: abc | def jkl... (4 Replies)
Discussion started by: salil2012
4 Replies

10. UNIX for Dummies Questions & Answers

OFS in awk

Hi, I have these out put field seperator changed to "|" in my awk command, but it didn't give me the result. Can someone help me find out why? ======================================= /bin/awk 'BEGIN { OFS="|" } { print $0 }' list.tmp.$$ > listtmp.$$ =======================================... (1 Reply)
Discussion started by: whatsfordinner
1 Replies
Login or Register to Ask a Question