Problem with AWK and OFS


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with AWK and OFS
# 8  
Old 10-20-2011
Quote:
Originally Posted by bartus11
If sequence is always last line in the file:
Code:
awk 'END{print ">"FILENAME"\n"$0}' Infile.seq

not all awk's can reference the $0 in the END block - AiX's doesn't.
As an alternative:
Code:
nawk '/^\^\^/ {print ">" FILENAME;f=1;next}f&&f--' myFile

# 9  
Old 10-20-2011
getline! Of course!
Thanks!

vgersh99,

Would you mind explaining me the last part (f=1;next}f&&f--) of your code?
Thanks!

X
# 10  
Old 10-20-2011
Quote:
Originally Posted by Xterra
getline! Of course!
Thanks!

vgersh99,

Would you mind explaining me the last part (f=1;next}f&&f--) of your code?
Thanks!

X
f=1 assign 1 to f as we need to print the next line. Had we had the need to print 3 lines below the matched pattern, f=3

f&&f-- if f>0, subtract 1 from f. When the boolean condition is true, the awk EXPLICIT action is print the current record. Which is the same as writing:
Code:
f&&f-- {print $0}

NOTE: I don't like using 'getline' as it breaks the logic/paradigm of data flow in awk which is process each record/line one at a time. I only use 'getline' when I need to 'read in' an additional lookup/config file inside awk in addition of parsing the specified file in the normal manner. It's a matter of taste I reckon...
This User Gave Thanks to vgersh99 For This Post:
# 11  
Old 10-20-2011
Guys,
Thank you so very much!
X
# 12  
Old 10-20-2011
Regarding the original code and why it failed, my guess is that your problem results from a misunderstanding of how RS is interpreted by your AWK.

POSIX states that only the first character of RS is used as the record separator. If RS consists of more than one character, the behavior is undefined.

Many AWK implementations (notably, the ones most likely to be found on a linux system, including gawk, mawk, and busybox's awk) treat a multicharacter RS as a regular expression.

So, one of two things is likely happening. Either your AWK is treating RS as the single character ^, in which case it's not splitting on the character sequence you intend. Or, your AWK is treating it as a regular expression, in which case it is also not splitting on the character sequence you intend.

If it's treating RS as a regular expression, what does ^^ mean? I'm not certain how AWK implementations which support regular expressions in RS handle anchors (^ and $), since in that context a newline isn't exactly meaningful (it is, afterall, what one is attempting to define when setting RS).

... after a few minutes spent dusting off and booting a 6 yr old version of debian with mawk ...

mawk supports multicharacter, regular expression RS values, and, as expected, RS='^^' does not split records on ^^. However, RS='\^\^' did the trick.

Regards,
Alister
This User Gave Thanks to alister For This Post:
 
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 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 $ cat file1 A1:B1:C1:NoLimit M1:M2:M3:Limit A2:B2:C2,C3,C4,C5 A3:B3:C3,C4,C5,C6,C7Desired output A1,B1,C1,NoLimit M1,M2,M3,Limit A2,B2,C2 ,,,C3 ,,,C4 ,,,C5 A3,B3,C3... (5 Replies)
Discussion started by: chidori
5 Replies

3. 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

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

Parsing XML in awk : OFS does not work as expected

Hi, I am trying to parse regular XML file where I have to reduce number of decimal points in some xml elements. I am using following AWK command to achive that : #!/bin/ksh EDITCMD='BEGIN { FS = ""; OFS=FS } { if ( $3 ~ "*\\.*" && length(substr($3,1+index($3,"."))) == 15 ) {... (4 Replies)
Discussion started by: martin.franek
4 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