While read with different delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While read with different delimiter
# 1  
Old 10-12-2016
While read with different delimiter

Hello all,

I have the following script:

Code:
#!/bin/bash
 i_string="TABLE|T_TS+AUSDRUCK|'hans.meier%'+SHIP|'Herr Kanns%'"
 while IFS="+" read key_value_item
do
  echo "key_value_item: $key_value_item"
done <<< $i_string

I would like to split the string and echo all the parts. The parts are delimited by '+'.

What I get is:

Quote:
key_value_item: TABLE|T_TS+AUSDRUCK|'hans.meier%'+SHIP|'Herr Kanns%'
But what I would like to have is:
Quote:
key_value_item: TABLE|T_TS
key_value_item: AUSDRUCK|'hans.meier%'
key_value_item: SHIP|'Herr Kanns%'
Do you have a solution?

CU
# 2  
Old 10-12-2016
Hello API,

Could you please try following and let us know if this helps you.
Code:
echo "key_value_item: TABLE|T_TS+AUSDRUCK|'hans.meier%'+SHIP|'Herr Kanns%'" |  awk '{Q=$1;$1="";num=split($0, A,"+");for(i=1;i<=num;i++){printf("%s %s\n",Q,A[i])};}'

Output will be as follows.
Code:
key_value_item:  TABLE|T_TS
key_value_item: AUSDRUCK|'hans.meier%'
key_value_item: SHIP|'Herr Kanns%'

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-12-2016 at 11:43 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-12-2016
Code:
#!/bin/bash

IFS="+" i_string="TABLE|T_TS+AUSDRUCK|'hans.meier%'+SHIP|'Herr Kanns%'"

for key_value_item in $i_string
do
  echo "key_value_item: $key_value_item"
done

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 10-12-2016
@RavinderSingh13: Yes - its working.

@rdrtx1: Yes. This works. I am surprised, that a for-loop is doing that.


Thanks for your help.
# 5  
Old 10-12-2016
Hi,
In bash (just for only one line) :
Code:
i_string="TABLE|T_TS+AUSDRUCK|'hans.meier%'+SHIP|'Herr Kanns%'"
while read -d"+" key_value_item || [[ -n "$key_value_item" ]]
do 
  echo "key_value_item: $key_value_item"
done <<<$i_string

Regards.
# 6  
Old 10-12-2016
Quote:
Originally Posted by API
@rdrtx1: Yes. This works. I am surprised, that a for-loop is doing that.
It's actually IFS that's doing it. By default, the shell splits strings on whitespace, but IFS controls that. Here it's splitting on pluses instead.
# 7  
Old 10-12-2016
If enough variables to be read into are supplied, the values are separated:
Code:
IFS="+" read var1 var2 var3 <<< $i_string
echo "item1: " $var1
echo "item2: " $var2
echo "item3: " $var3

item1:  TABLE|T_TS
item2:  AUSDRUCK|'hans.meier%'
item3:  SHIP|'Herr Kanns%'

or, use an array:

Code:
IFS="+" read -a ARR <<< $i_string
echo ${#ARR[@]}
3
echo ${ARR[1]}
AUSDRUCK|'hans.meier%'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 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

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

8. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

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

10. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies
Login or Register to Ask a Question