[SOLVED] How to set field separator in awk when FS is "||" ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] How to set field separator in awk when FS is "||" ?
# 1  
Old 12-14-2012
[SOLVED] How to set field separator in awk when FS is "||" ?

Dear Experts,

How to use field separator in awk , when the field separation is needed as "||"



Example: (file1 )

Code:
aa || bb  ||  cc || 1234
xx || yy  ||  zz  || 1123





Q:

If I want to print $3 and $4 only , with using the field separator "||"
How to write awk code:

I am trying :

Code:
#  awk -F "||" '{print $3 , $4 }  file1



But not working,

Thanks in advance,
Rveri.

Last edited by Corona688; 12-14-2012 at 05:47 PM..
# 2  
Old 12-14-2012
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-14-2012
Does your input file really look like that, or did you fill it with spaces to try and make it align?

What's your system? Many versions of awk will only accept a single character as the input separator.

In what way is it "not working"?
# 4  
Old 12-14-2012
Hi Corona688,

Thank you for quick reply,
The OS is: hp-ux 11.23 , IA64, I have given the information precisely again:


The data file:
Code:
# cat file1
aa || bb || cc || dd  || 1234
xx || yy || zz || ww  || 0911
bb || xx || bb || tt  || 2322



I am trying here to get two filed, say $3 & $4 . But it is not giving the desired output: for $3 & $4 values.

Code:
# cat file1 | awk -F "||" '{print $3,$4}'
awk: There is a regular expression error.
        Unknown error
 The input line number is 1.
 The source line number is 1.




Code:
# what /usr/bin/awk
/usr/bin/awk:
         lib.c $Date: 2005/10/05 03:13:21 $Revision: r11.23/1 PATCH_11.23 (PHCO_33931)
         $Revision: @(#) awk R11.23_BL2005_1214_2 PATCH_11.23 PHCO_33931 
#


Thank you,
# 5  
Old 12-14-2012
Regular expression error? Perhaps your awk can take multiple characters after all, if it's considering it as a regular expression.

It may be taking the | to mean 'or' instead of a literal |.

Code:
awk -F '[|][|]' '{ print $3,$4 }' filename

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-14-2012
Right the awk was assuming it was an or operator like "||" OR operator,
Thanks for showing the correct usage , it worked like a charm . SOLVED!.
# 7  
Old 12-14-2012
| has a slightly different meaning in a regex, used like abc|def|ghi to match multiple different strings

But same problem either way, have to tell it to take it literally.
This User Gave Thanks to Corona688 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

Why awk print is strange when I set FS = " " instead of FS = "\t"?

Look at the following data file(cou.data) which has four fields separated by tab. Four fields are country name, land area, population, continent where it belongs. As for country name or continent name which has two words, two words are separated by space. (Data are not accurately... (1 Reply)
Discussion started by: chihuyu
1 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk: Force "escaped" representation of a field

Is there any way to get the escaped version of a field in awk? For those that don't understand the question, here is a clarifying example. Lets say a field $1 gives me the string "(dumb'" (I've changed the delimiter to be something other than whitespace). If i use that value in a command in awk... (2 Replies)
Discussion started by: IvanMalison
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

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

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

Problem with "set" and "awk"

Hi, i'm programming on /bin/csh and i need to get the number extracted by this: set ppl_kn = $(awk '{ field = $6 } ; END{ print field }' < ppl_LM_kn.ppl ) and the output is: "Illegal variable name." Please anyone can help me what's wrong? Thanks in advance (2 Replies)
Discussion started by: tmxps
2 Replies
Login or Register to Ask a Question