Specifying IFS delimiter in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specifying IFS delimiter in while loop
# 1  
Old 07-21-2016
Specifying IFS delimiter in while loop

i have data that is delimited with #x#:

Code:
file1#x#file2#x#file3
file4#x#file5#x#file6

data is stored in a variable called ALLMYDATA:

Code:
echo "${ALLMYDATA}" | while IFS="#x#" read -r line junk
do
echo ${line}
done

it appears IFS does not allow the specification of more than one character as a delimiter. is there a way around this? im looking for a solution that does not make any external calls to system tools. i dont want to awk or perl this.

I'd like to use available shell functions.
# 2  
Old 07-21-2016
IFS can be more than one character ie. a string, but not a group of characters.

You want to do something like this?

Code:
$ OLDIFS=$IFS
$ IFS="#x#"
$ ALLMYDATA="file1#x#file2#x#file3
file4#x#file5#x#file6"
$ echo $ALLMYDATA | while read LINE; do IFS=$OLDIFS; echo $LINE| while read A B C; do echo "$A -- $B -- $C";done ; done
file1 -- file2 -- file3
file4 -- file5 -- file6

These 2 Users Gave Thanks to zaxxon For This Post:
# 3  
Old 07-21-2016
If you are using bash, would you be better with this as your loop?:-
Code:
ALL_MY_DATA=$(sed 's/#x#/ /g' <<< $ALLMYDATA)                    # Replace the three characters with a space all through the record
for LINE in $ALL_MY_DATA
do
   echo $LINE
done

However it seems that you might only want the first field of each row. Is that right?

If so, then you could perhaps work on a variation of:-
Code:
while read LINE
do
   echo "${LINE%%#x#*}"                                          # Trim off everything after the first #x#
done <<< "$ALLMYDATA"

If you are using ksh we can work it out for that too.



I hope that this helps,
Robin

Last edited by rbatte1; 07-21-2016 at 07:51 AM.. Reason: Added comments
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 07-21-2016
How about (bash!)
Code:
echo "${ALLMYDATA//#x#/ }" | while read -r line junk; do echo $junk, $line; done

?
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 07-21-2016
Quote:
Originally Posted by RudiC
How about (bash!)
Code:
echo "${ALLMYDATA//#x#/ }" | while read -r line junk; do echo $junk, $line; done

?
this is ideal however, its not portable. some OSes dont have bash..i.e. sunos and aix.

---------- Post updated at 09:30 AM ---------- Previous update was at 09:28 AM ----------

Quote:
Originally Posted by rbatte1
If you are using bash, would you be better with this as your loop?:-
Code:
ALL_MY_DATA=$(sed 's/#x#/ /g' <<< $ALLMYDATA)                    # Replace the three characters with a space all through the record
for LINE in $ALL_MY_DATA
do
   echo $LINE
done

However it seems that you might only want the first field of each row. Is that right?

If so, then you could perhaps work on a variation of:-
Code:
while read LINE
do
   echo "${LINE%%#x#*}"                                          # Trim off everything after the first #x#
done <<< "$ALLMYDATA"

If you are using ksh we can work it out for that too.



I hope that this helps,
Robin
im not only looking for the first field. its just in the example i provided, the first field happened to be the field i was looking for. im going to be interested in all fields.

---------- Post updated at 09:48 AM ---------- Previous update was at 09:30 AM ----------

Quote:
Originally Posted by rbatte1
If you are using bash, would you be better with this as your loop?:-
Code:
ALL_MY_DATA=$(sed 's/#x#/ /g' <<< $ALLMYDATA)                    # Replace the three characters with a space all through the record
for LINE in $ALL_MY_DATA
do
   echo $LINE
done

However it seems that you might only want the first field of each row. Is that right?

If so, then you could perhaps work on a variation of:-
Code:
while read LINE
do
   echo "${LINE%%#x#*}"                                          # Trim off everything after the first #x#
done <<< "$ALLMYDATA"

If you are using ksh we can work it out for that too.



I hope that this helps,
Robin

Code:
ALL_MY_DATA=$(sed 's/#x#/ /g' <<< $ALLMYDATA)                    # Replace the three characters with a space all through the record
for LINE in $ALL_MY_DATA
do
   echo $LINE
done

this seems to work but it appears if the data contains an "x" it gets read of it and replaces it with a space.
# 6  
Old 07-21-2016
Quote:
Originally Posted by SkySmart
Code:
ALL_MY_DATA=$(sed 's/#x#/ /g' <<< $ALLMYDATA)                    # Replace the three characters with a space all through the record
for LINE in $ALL_MY_DATA
do
   echo $LINE
done

this seems to work but it appears if the data contains an "x" it gets read of it and replaces it with a space.
No. That sed command will not change x to a space unless it is surrounded by octothorps (unless you also still have x as a character in IFS). If $IFS expands to a string containing x, then the field splitting performed when expanding $ALL_MY_DATA in the line:
Code:
for LINE in $ALL_MY_DATA

will discard any x characters remaining in ALL_MY_DATA after the sed completes.

After running your earlier code, did you remember to reset IFS to the default <space>, <tab>, and <newline> before running the above code?
This User Gave Thanks to Don Cragun 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

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

3. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

4. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

5. Shell Programming and Scripting

While loop and IFS?

Hi, while ; do echo "Please enter " read enter yyyy=${enter:0:4} mm=${enter:5:2} dd=${enter:8:2} result=`validateDate $yyyy $mm $dd` When does the loop keeping repeating till?? till 1 is equal to 1? what does this mean "${enter:0:4}" .The 0 and 4 part?? ... (3 Replies)
Discussion started by: sid22
3 Replies

6. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

7. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

8. Shell Programming and Scripting

while loop with 3 ifs

im messing up somehwere...and can't seem to clean up the script...for it to work objectives: 1. check for today's file, and sleep 30 secs between retries 2. only allow 5 tries before script should fail. 3. if today's file found, wait 30 seconds for it to process.. code: count=0... (8 Replies)
Discussion started by: sigh2010
8 Replies

9. Shell Programming and Scripting

Preventing whitespace to be a delimiter in a for loop (bash/sh)

Hi, I have a for loop which iterates over a list of strings, separated by whitespace: $ list="1 2 3" $ for i in $list; do echo $i; done 1 2 3 I now want to introduce some strings containing whitespace themselves ... This is straightforward if I directly iterate over the list: $ for... (4 Replies)
Discussion started by: kkkoehne
4 Replies

10. Shell Programming and Scripting

regarding IFS=

hi i am a learner can some explain "export IFS=$(echo "\n\t\a")" i am not able to understand the functionality please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies
Login or Register to Ask a Question