Read character and use as separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read character and use as separator
# 1  
Old 05-06-2015
Read character and use as separator

Hi all,

I'm trying to read in a character and use it as a separater on a string:

Code:
#!/bin/ksh

echo "Enter input line"
read input_header

echo "Enter separator:"
read separator

IFS="$separator" read -A fields <<< "$input_header"

for ((i=0;i<${#fields[@]};i++)) ; do
  echo ${fields[$i]}
done

This works when using characters like comma's etc. But not with spaces and tabs:

Code:
-bash-4.1$ ./test.ksh
Enter input line
test1,test2,test3
Enter separator:
,
test1
test2
test3
-bash-4.1$ ./test.ksh
Enter input line
test1 test2 test3
Enter separator:
   <- space
test1 test2 test3

Is there any way to do this?

Thanks
# 2  
Old 05-06-2015
Try IFS="" read -r separator
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-06-2015
Quote:
Originally Posted by Corona688
Try IFS="" read -r separator
Thanks! Works great
This User Gave Thanks to Subbeh For This Post:
# 4  
Old 05-06-2015
" " (space) is one of the default IFS chars and thus is removed during read. An alternative to corona688's modifying the IFS is escaping the space in the input line:
Code:
./shscr
Enter input line
test1 test2 test3
Enter separator:
\                             # 1 escaped space here
test1
test2
test3

This User Gave Thanks to RudiC 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

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

2. Shell Programming and Scripting

read into a range of character

i have this problem: i must hide a string with a character such as _ by command WORD=string; XXX=`echo $WORD | sed 's//_/g' but after, users must send in input a character and i must to replace the _ with the input character or better i can do this -$CHARS_INPUT i have think to use command... (3 Replies)
Discussion started by: tafazzi87
3 Replies

3. Shell Programming and Scripting

Read character by character

Guys, Here is the input text file <7001> 34 789 701 2 <HJS1> 2 <HJS2> 2 <HJS3> ... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

4. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

5. UNIX for Dummies Questions & Answers

read from character 23 through character 32

I need a one-liner that will output characters 23 through 32 from a user defined record. Thanks, Kenny. (1 Reply)
Discussion started by: kenneth.mcbride
1 Replies

6. Shell Programming and Scripting

How to read character by character in a file

Hi, How read character by character from a file . and i need replace '.' with null if it comes as a 5 character i am beginner ...please help me (1 Reply)
Discussion started by: kartheek
1 Replies

7. Shell Programming and Scripting

Can I read a file character by character?

Hello all respected people, Can i read a file character by character without using sed,awk and perl commands. Thanks in advance. (4 Replies)
Discussion started by: murtaza
4 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

10. Shell Programming and Scripting

Can i read a file character by character

How to read character by character using awk (6 Replies)
Discussion started by: karnan
6 Replies
Login or Register to Ask a Question