Splitting text string with "|" pipe delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting text string with "|" pipe delimiters
# 1  
Old 06-03-2010
Splitting text string with "|" pipe delimiters

Hi,

I need to split the fields in some text strings into separate variables.
The fields are separated by pipes.

The strings look roughly like this:
Code:
CUSTNAME|hostname|userid|gecos field|staff|disabled||SUDO

Currently I can do it using something like cut -d"|" -f1, but when I put that into a while loop (need to go through the text file line by line), it stops working.

Here is the code I have now:
Code:
while read line
do
O1=`cut -d '|' -f1 $line`
O2=`cut -d"|" -f2 $line`
"
"
O8=`cut -d"|" -f8 $line`

echo "Field O1 is $O1"
echo "O2 is \n$O2"
"
"
echo "O8 is \n$O8"
done < listing.txt


I'm wondering if it is the presence of the pipe character which is confusing the shell.

I'm willing to try any alternative approach people can suggest. I know this should be simple, but I just can't get it to work.

Any help VERY much appreciated.
TIA, Alan.

Last edited by Franklin52; 06-03-2010 at 03:07 AM.. Reason: Please use code tags!
# 2  
Old 06-03-2010
Code:
O1=`cut -d '|' -f1 $line`
O2=`cut -d"|" -f2 $line`

In your code shell is treating $line as a file input.Thats why its failing. I have made a slight change in code.

Code:
while read line
do
O1=`echo $line |cut -d '|' -f1 `
O2=`echo $line |cut -d"|" -f2 `
O8=`echo $line |cut -d"|" -f8 `

echo "Field O1 is $O1"
echo "O2 is \n$O2"
echo "O8 is \n$O8"
done < listing.txt

# 3  
Old 06-03-2010
can be done so
Code:
FIELDS=( F1 F2 F3 F4 F5 F6 F7 F8 )
IFS='|'
while read ${FIELDS[@]}
do
    for F in ${FIELDS[@]}
    do
        echo "$F is ${!F}"
    done
done < listing.txt

# 4  
Old 06-03-2010
From a file you can use:
Code:
while IFS='|' read O1 O2 O3 O4 O5 O6 O7 O8; do
  echo "Field O1 is $O1" 
  echo "Field O2 is $O2"
done<listing.txt

From a string you can use:
Code:
string="CUSTNAME|hostname|userid|gecos field|staff|disabled||SUDO"
IFS='|' read O1 O2 O3 O4 O5 O6 O7 O8 <<EOF
$string
EOF

or in bash/ksh93:
Code:
IFS='|' read O1 O2 O3 O4 O5 O6 O7 O8 <<<"$string"



---------- Post updated at 08:52 ---------- Previous update was at 08:45 ----------

In ksh93/bash you can also use arrays, like so:
Code:
 IFS='|' O=( $string )

Code:
$ echo ${O[4]}
staff
$ echo ${O[0]}
CUSTNAME

# 5  
Old 06-03-2010
Thanks for the replies amitranjansahu, frans and Scrutinizer.

amitranjansahu, I saw yours first, and tried it. That works perfectly, thanks.

frans and Scrutinizer, thanks for the alternative methods. Very interesting - I will save those for possible future situations.

You guys are great Smilie. Cheers, Alan.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

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

6. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

7. Shell Programming and Scripting

sort text having delimiter with "|" (pipe)

i am having text file below NARGU S S 12358 SALES REP |22| Acccount/s RAJU R B 64253 SALES REP |12| Acccount/s RUKMAN S 32588 SALES REP |10| Acccount/s NARGUND S S 12356... (3 Replies)
Discussion started by: suryanarayana
3 Replies

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

9. Shell Programming and Scripting

get string out of text file after "= "

I have a text file like: The name of the project = "Symbion centrifuge" The name of the subsytem = "motor demon" now i would like to read the string after the = into a variable to write it to another file. later use like: sed s/'PROJECTNAME'/"$projectname"/ <header.tex >temp ... (9 Replies)
Discussion started by: vivelafete
9 Replies

10. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies
Login or Register to Ask a Question