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')
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:
then the output from
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:
(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:
instead of
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:
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 ("|").
Thanks a lot, it's woks perfectly, can you please explain about the following code.
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.
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)
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)
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)
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)
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)
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)
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)
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)