AWK FS with ignore space option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK FS with ignore space option
# 1  
Old 08-29-2012
AWK FS with ignore space option

Hi,
i have in a situation to get the variables which are having a "|" delimiter and each variable may or may not have spaces, so how can i skip the spaces on the string and print the string as it is.

For example:
line = "|Hello world|Hello|Hi Guys|
read x y z <<<$(echo "$line" | awk -F"|" '{print $1,$2,$3}')
echo -e "$x" (should be 'Hello world')

buts it's printing only Hello.

Could some one please help me.
# 2  
Old 08-29-2012
No awk needed, try this:

Code:
line = "|Hello world|Hello|Hi Guys|"

OLDIFS="$IFS"
IFS="|"
set -- $line
IFS="$OLDIFS"

echo "$2"
echo "$3"
echo "$4"

# 3  
Old 08-29-2012
Or this, since you have BASH:

Code:
IFS="|" read A B C D E <<<"|a b|c d|e f|g h"

# 4  
Old 08-29-2012
Quote:
Originally Posted by Corona688
Or this, since you have BASH:

Code:
IFS="|" read A B C D E <<<"|a b|c d|e f|g h"

Thanks a lot, it's woks perfectly, can you please explain about the following code.
Code:
OLDIFS="$IFS"
IFS="|"
set -- $line
IFS="$OLDIFS"


Last edited by Franklin52; 08-30-2012 at 04:34 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 08-29-2012
Quote:
Originally Posted by tprabhaker
Hi,
i have in a situation to get the variables which are having a "|" delimiter and each variable may or may not have spaces, so how can i skip the spaces on the string and print the string as it is.

For example:
line = "|Hello world|Hello|Hi Guys|
read x y z <<<$(echo "$line" | awk -F"|" '{print $1,$2,$3}')
echo -e "$x" (should be 'Hello world')

buts it's printing only Hello.

Could some one please help me.
Since you don't have a closing double-quote at the end of the 1st line of this script, I'm VERY surprised that you didn't get a syntax error.

What corona688 suggested should get you what you want. But, assuming you had supplied the closing double quote, let's evaluate what is going on so you may understand why you would get "Hello" assigned to x instead of "Hello World". So, assuming we start with the script:
Code:
line = "|Hello world|Hello|Hi Guys|"
read x y z <<<$(echo "$line" | awk -F"|" '{print $1,$2,$3}')
echo -e "$x" (should be 'Hello world')

then the output from
Code:
echo "$line" | awk -F"|" '{print $1,$2,$3}'

on line2 will be the string" Hello World Hello\n" because with your input field separator set to "|" in awk, $1 will be set to an empty string (what appeared in $line before the 1st "|"), $2 will be set to "Hello World", and $3 will be set to "Hello". The awk print command adds an output field separator (in this case a space) between the expansion of $1 and $2, another separator between the expansion of $2 and $3, and adds a newline terminator after the expansion of $3.

So after we're done with awk, line2 in the script is essentially:
Code:
echo " Hello World Hello"|read x y z

(ignoring the fact that the read could be executed in a subshell on some systems). Now, since you haven't given us any indication that you've changed the setting of IFS we'll assume that it the default string containing a space, a tab, and a new-line character. So read will split the line it reads and set x to Hello, y to World, and z to the remainder of the line up to the terminating new-line (which in this case is the final Hello).

So line 3 in your script echo $x (the -e option makes no difference since you know in this script that the string passed to echo can't contain any backslash escape sequences) prints "Hello" and adds a line terminating new-line character.

IFS is the shell's input field separator character list. Note that in the last post by corona688, he set IFS to "|", but only for the read command. If the command he gave had been:
Code:
IFS="|" ; read A B C D E <<<"|a b|c d|e f|g h"

instead of
Code:
IFS="|" read A B C D E <<<"|a b|c d|e f|g h"

the setting of IFS would have applied to the rest of the line parsing performed by this shell instead of just the line parsin performed by the read.

The command sequence:
Code:
OLDIFS="$IFS"
IFS="|"
...
IFS="$OLDIFS"

saves the current value of IFS in OLDIFS, sets IFS to the vertical-bar
runs whatever is specified by ... and then restores the original value of IFS.

The set -- $linesets the shell's positional parameters ($1, $2, ... $n) and $# by splitting the value stored in the line variable using the current field separator ("|").
# 6  
Old 08-30-2012
Quote:
Originally Posted by tprabhaker
Thanks a lot, it's woks perfectly, can you please explain about the following code.
Code:
OLDIFS="$IFS"
IFS="|"
set -- $line
IFS="$OLDIFS"

IFS is a special variable which controls string splitting in shell. Ordinarily it splits on spaces, but it can split on other things as well. So:

1) Save default value of IFS for later.
2) Set IFS to |
3) Set the $1 $2 $3 $4 variables to $line, splitting on |
4) Restore IFS to its original value

You can also use IFS in concert with READ, like

IFS="|" read A B C D where read will split upon |, not whitespace. Because IFS is prefixed to that line, IFS will be changed only for that line and does not need to be restored. This doesn't work for set for some reason.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to ignore whitespace in field

The awk below executes and update the desired field in my first awk. However, the white space between nonsynonymous SNV in $9 is being split into tabs and my attempt to correct this does not update the field unless it is removed. I am not sure what I am doing wrong? Thank you :). file1 ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

3. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

4. Shell Programming and Scripting

ignore space regular expression

I just wrote a modsecurity rule that blocks execution on "cat /etc/passwd" from webshell. But when I use cat /etc/passwd it works. Ie when I add space after cat. What I need is a regular expression to ignore additional space than the first single space after cat. (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

Ignore case in awk while pattern searching

Hi , I have the file where i have to search for the pattern. The pattern may be lower case or upper case or camel case. Basically I want to ignore while searching the pattern in awk. awk '/error|warning/exception/' filename Please help me (3 Replies)
Discussion started by: arukuku
3 Replies

6. Shell Programming and Scripting

awk to ignore the text before a particular word

Hi I am new to Awk programming , i would appreciate if anyone help me with the below scenario i have text file arranged in rows and columns like below 11004 04493384 26798 CASSI0000I Server manager initialization started 111004 04493486 26798 CASSI4005I Retrieving ES... (7 Replies)
Discussion started by: rakeshkumar
7 Replies

7. Shell Programming and Scripting

Ignore records with no values through awk

Hi Guys, Hope you are doing well out there. I have to format the output of a script. Current output is auktltbr.dc-dublin.de:4322 ICCIR2Test13-PB-01 active auktltbr.dc-dublin.de:8322 ICCIR2Test13-SB-02 active auktlttr.dc-dublin.de:4422 ICCIR2Test24-CB-02 active... (10 Replies)
Discussion started by: singh.chandan18
10 Replies

8. Shell Programming and Scripting

Space Monitoring option

thrld=`bdf /u01/|grep u01|awk {'print $5'}|sed 's/'%'//'` thrld1=`bdf /u12/|grep u02|awk {'print $5'}|sed 's/'%'//'` ch="" echo "------------------" echo " " echo "------------------" echo "1)/u01" echo "2)/u12" echo " Please select : \c" read ch case "$ch" in... (3 Replies)
Discussion started by: killboy
3 Replies

9. Shell Programming and Scripting

awk, ignore first x number of lines.

Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file. As it is now I run this command: ... (8 Replies)
Discussion started by: trey85stang
8 Replies

10. Shell Programming and Scripting

Trick to ignore space in folder name

Hello All, I am getting error while passing a folder name that has space to the cmd line argument. sh log_delete2.sh "/home/kumarpua/TESTARTIFACTS/atf-chix/ATF-subversion-dev/ssenglogs/A RM" log_delete2.sh: line 17: cd: /home/kumarpua/TESTARTIFACTS/atf-chix/ATF-subversion-dev/ssenglogs/A:... (3 Replies)
Discussion started by: pulkit
3 Replies
Login or Register to Ask a Question