yet another awk field syntax question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting yet another awk field syntax question
# 1  
Old 03-22-2007
yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz'

Code:
#cat abc
test1:test2:xyz:test3:test4:test5

#cat def
test1:test2:test3:xyz:test4:test5

desired output is to be able to print NF and any trailing fields separated by':'

test3 3
or
test4 3
or
test5 3

likewise,

test4 2
or
test5 2

placeing a field separator as 'xyz' illiminates using ':' as a second separator or is that possible?
# 2  
Old 03-22-2007
Code:
awk -F: -v OFS="\nor\n" ' { sub(".*xyz:",""); for(i=1;i<=NF;++i) $i=$i" "NF ;print }  ' file

# 3  
Old 03-22-2007
I'm gonna use sed for the time being, but would like to know the correct awk syntax as well

cat abc | sed 's/\(.*\)xyz://' | awk -F: '{print $1,NF}'

edit*

Quote:
Originally Posted by anbu23
Code:
awk -F: -v OFS="\nor\n" ' { sub(".*xyz:",""); for(i=1;i<=NF;++i) $i=$i" "NF ;print }  ' file

thanks, i'll try it
# 4  
Old 03-22-2007
Quote:
Originally Posted by prkfriryce
I'm gonna use sed for the time being, but would like to know the correct awk syntax as well

cat abc | sed 's/\(.*\)xyz://' | awk -F: '{print $1,NF}'

edit*



thanks, i'll try it
You can do everything in awk.Check my previous post
# 5  
Old 03-22-2007
Quote:
Originally Posted by anbu23
You can do everything in awk.Check my previous post
yeah works well, a little too well! i was looking for a more simplified version by printing the fields individually, but the 'sub' syntax is what i was looking for. I'll tweek it for future applications.

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

2. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

3. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

4. Shell Programming and Scripting

OR operator syntax question in AWK script

Hi guys, I confused about syntax used in OR script as follow: I have this sample file separated by "|" containing: January|Month No. 1 February|Month No. 2 March|Month No. 3 April|Month No. 4 May|Month No. 5 June|Month No. 6 July|Month No. 7 August|Month No. 8 September|Month No. 9... (11 Replies)
Discussion started by: cgkmal
11 Replies

5. Shell Programming and Scripting

simple awk question: split field with :

Hi, Probably a very weak question.. but I have tried all I know.. BPC0001:ANNUL_49542 0.0108 -0.0226 -0.0236 0.0042 0.0033 -0.0545 0.0376 0.0097 -0.0093 -0.032 Control BPC0002:ANNUL_49606 0.0190 -0.0142 -0.0060 -0.0217 -0.0027 ... (3 Replies)
Discussion started by: genehunter
3 Replies

6. Shell Programming and Scripting

awk syntax question

Hi I use awk command to delete the first blanc line of a file: awk '/^$/ && !f{f=1;next}1' infile > outfile can somebody please explain me what the last "1'" in !f{f=1;next}1' stands for... Thansk a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies

7. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies

8. Shell Programming and Scripting

field seperator question (awk script)

Is there a way I could use different a different field seperator for different parts of the body? kinda like {FS = ":"} FILENAME == "products"{ price = $3 if(numprods < $1-100) numprods = $1-100 } {FS = "/"}{} FILENAME == "associates"{ associateid... (5 Replies)
Discussion started by: angermanaged
5 Replies

9. UNIX for Dummies Questions & Answers

AWK syntax question

Hi, Have to check file names in some given directory. SO, What is the right syntax here: *$3*=="'$object_list'" - just wanted to check if $3 is in the object_list. And also, Do I need so many quotes around? (5 Replies)
Discussion started by: Leo_NN
5 Replies

10. Shell Programming and Scripting

awk syntax question

Hi there could someone explain what is happening in the following function/statement for me, im just a little confused code = 'BEGIN{FS=","} { printf ("%-11s,%s%s%s,%07.2f,%14s,%-3s\n",$1,substr($2,9,2),substr($2,6,2),substr($ 2,3,2),$9,$10,$12) } this function is called later in the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question