Two delimiters with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two delimiters with AWK
# 1  
Old 01-27-2011
Two delimiters with AWK

Hello,
this thread is more about scripting style than a specific issue.
I've to grep from a output some lines and from them obtain a specific entry delimited by < and >.

This is my way :
Code:
1) grep -i user list | awk '{FS="<";print $NF}' | sed -e 's/>//g'
 
2) grep -i user list | cut -d"," -f 3 | cut -d '<' -f 2 | sed -e 's/>//g'

Now, I want to know if there is a best way (sure there is Smilie ) to do it.

Moreover, it is possible to put all instruction on a awk cmd ?

br/gb

--------------------------------
UPDATE -
A third way solution :
Code:
grep -i user list | awk -F"[<,>]"  '/>/{print $(NF-1)}'

It is fine... but not complete, cause NF is a blanck char. How to del this char from awk test ?

Last edited by gogol_bordello; 01-27-2011 at 10:20 AM..
# 2  
Old 01-27-2011
As always, to help us understand your requirements,
post some small representative sample input and the output
you'd like to get given that input.
# 3  
Old 01-27-2011
These pop off the top of my head:

Code:
# if only one set of <....> on each line, then this will work
awk '
    {
        gsub( ".*<", "", $0 );    # delete all before open
        gsub( ">.*", "", $0 );    # delete all after close
        print;
    }
'

# if there might be more than one <....> per line, this will do the trick printing
# the contents of each <....>  1 per line
awk '
    {
        n = split( $0, a, "<" );      # split the input line based on open
        for( i = 2; i <= n; i++ )    # for each open; skip things before first hence i=2
        {
            gsub( ">", "", a[i] );     # delete everything after close
            print a[i];
        }
    }
'

# and from the category of "stupid sed tricks" this wil
# also handle multiple <...> per line, and print the contents 
# one per output line. 

sed 's/^[^<]*</</; s/>[^<]*</></g; s/>[^<]*$/>/;  s/></\n/g; s/<//; s/>//'

I won't offer an explanation of the sed on the off chance that you'd like to figure it out on your own!
# 4  
Old 01-28-2011
First of all, my code works. I want only to know if exist a better way to do it.

Then

Quote:
Originally Posted by radoulov
As always, to help us understand your requirements,
post some small representative sample input and the output
you'd like to get given that input.
Here two differents grepped lines :
Code:
1) Job <0123456>, Job Name <Test_A>, User <anon>,
 
2) Job <6543210>, Job Name <Test_B>, User <nona>

Code used (that I want to perform) :
Code:
grep -i user list | awk -F"[<,>]"  '{ print $(NF-1) }'

Results :
Code:
 
1) anon
 
2) nona

Grazie mille Smilie
# 5  
Old 01-28-2011
Two questions:

1. Are 1 ) and 2 ) part of the desired output,
or are there only for clarity?

2. Which operating system are you using?
# 6  
Old 01-28-2011
Quote:
Originally Posted by agama
These pop off the top of my head:

Code:
# if only one set of <....> on each line, then this will work
awk '
    {
        gsub( ".*<", "", $0 );    # delete all before open
        gsub( ">.*", "", $0 );    # delete all after close
        print;
    }
'
 
# if there might be more than one <....> per line, this will do the trick printing
# the contents of each <....>  1 per line
awk '
    {
        n = split( $0, a, "<" );      # split the input line based on open
        for( i = 2; i <= n; i++ )    # for each open; skip things before first hence i=2
        {
            gsub( ">", "", a[i] );     # delete everything after close
            print a[i];
        }
    }
'
 
# and from the category of "stupid sed tricks" this wil
# also handle multiple <...> per line, and print the contents 
# one per output line. 
 
sed 's/^[^<]*</</; s/>[^<]*</></g; s/>[^<]*$/>/;  s/></\n/g; s/<//; s/>//'

I won't offer an explanation of the sed on the off chance that you'd like to figure it out on your own!
Sorry, by I don't think it is a better way to accomplish the task... look at output :

Code:
grep -i user VAR_FILE | ./awkline
0123456, Job Name
Test_A, User
anon,

I need only anon

Moreover... I want to do everything with awk... without greep too. Smilie

thanks

---------- Post updated at 02:14 PM ---------- Previous update was at 02:12 PM ----------

Quote:
Originally Posted by radoulov
Two questions:

1. Are 1 ) and 2 ) part of the desired output,
or are there only for clarity?

2. Which operating system are you using?
1. Only for clarity

2. RH -> ops, HPUX 11.23
# 7  
Old 01-28-2011
Try:

Code:
awk '/[Uu][Ss][Ee][Rr]/ {print $(NF-1)}' FS='(<)|(>)' list

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delimiters with awk?

I have a file which is separated by delimiter "|", but the prob is one of my column do contain delimiter as description so how can i differentiate it? PS : the delmiter does have backslash coming before it, if occurring in column Annual|Beleagured|Desc|Denver... (2 Replies)
Discussion started by: nikhil jain
2 Replies

2. Shell Programming and Scripting

Count The Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command... (8 Replies)
Discussion started by: BrahmaNaiduA
8 Replies

3. UNIX for Dummies Questions & Answers

Why awk removes delimiters?

Code : echo "1,2,3,4"|awk -F "," 'NR==n{$3=a}1' n=1 a=45 Output : 1 2 45 4 Expected : 1,2,45,4 (4 Replies)
Discussion started by: Rajesh_us
4 Replies

4. Shell Programming and Scripting

awk multiple delimiters

Hi Folks, This is the first time I ever encountered this situation My input file is of this kind cat input.txt 1 PAIXAF 0 1 1 -9 0 0 0 1 2 0 2 1 2 1 7 PAIXEM 0 7 1 -9 1 0 2 0 1 2 2 1 0 2 9 PAKZXY 0 2 1 -9 2 0 1 1 1 0 1 2 0 1 Till the sixth column (which is -9), I want my columns to... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

5. Shell Programming and Scripting

Use two delimiters in awk

I have a file having lines like: 14: <a="b" val="c"/> 18: <a="x" val="d"/> 54: <a="b" val="c"/> 58: <a="x" val="e"/> I need to create a file with output: 14 d 54 e So basically, for every odd line I need 1st word if delimiter is ':' and for every even... (14 Replies)
Discussion started by: shekhar2010us
14 Replies

6. Shell Programming and Scripting

Delimiters in awk

Line from input file a : b : c " d " e " f : g : h " i " j " k " l output k b a Its taking 7th word when " is the delimiter, 2nd and 1st word when : is the delimiter and returning all in one line.... I am on solaris Thanks..... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

7. UNIX for Dummies Questions & Answers

comparing two files having different delimiters using awk

hi, i have a file called file1.txt and it's contents are as below: file1.txt: ------- abc,123, thomas dab,234,muller gab,456,ram The lookup file's contents are as below: lookup.txt ---------- abc|japan dcd|US dab|china gab|brazil (3 Replies)
Discussion started by: amar1003
3 Replies

8. Shell Programming and Scripting

AWK with multiple delimiters

I have the following string sample: bla bla bla bla bla I would like to extract the "123" using awk. I thought about awk -F"]" '{ print $1 }' but it doesn't work Any ideas ? (7 Replies)
Discussion started by: gdub
7 Replies

9. Shell Programming and Scripting

two delimiters with awk, one of them blank

Hello, I need use comma and spaces as field delimiters, but I can't: text: hello myfriend,I need,some help I need something like: awk -F"<blank>|," '{print $1, $3}' Thanks (3 Replies)
Discussion started by: albertogarcia
3 Replies

10. Shell Programming and Scripting

awk - treat multiple delimiters as one

Is there anyway to get awk to treat multiple delimiters as one? Particularly spaces... (6 Replies)
Discussion started by: peter.herlihy
6 Replies
Login or Register to Ask a Question