Parse a line by a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse a line by a space
# 1  
Old 09-10-2010
Parse a line by a space

I am trying to show each word on a separate line, but read in all the words on one command line. Below is the code I have right now:

Code:
read name
MyString=$name
name2=" "
echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
        if [ "$char" = "name2" ]
        then
            echo "$final"
            echo "in here"
            final=""
        else
            final=`echo $final $char`
        fi      
done

For some reason it is not working. What am I doing wrong?

Want to read in:

testing this program

and want to show on the screen

testing
this
program

Any help would be appreciated.
# 2  
Old 09-10-2010
Code:
tr -s ' '  '\n' < inputfile

Try that
# 3  
Old 09-10-2010
Try out this to replace space with new lines
Code:
echo $name | tr '\040' '\012'

Moderator's Comments:
Mod Comment Use code tags please, thanks.

Last edited by zaxxon; 09-10-2010 at 08:28 AM..
# 4  
Old 09-10-2010
itslee - i tried that and its good, but now I want to put each of those words into an array. So i can do some things on each of those strings passed etc.
# 5  
Old 09-10-2010
What shell do you use?
# 6  
Old 09-10-2010
its on a linux server. #!/bin/bash
# 7  
Old 09-10-2010
Code:
#!/bin/bash

read -a ARR

# echo all elements of array ARR
echo ${ARR[*]}

# echo element no. 2 of array ARR (Index starts at 0)
echo ${ARR[1]}

# echo number of elements
echo ${#ARR[*]}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

2. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

3. Shell Programming and Scripting

Parse the next line

If you have a file like this Mike Student:1:4 Boy Student:3:4 Girl :master Then Output is: Mike Student:1:4 Boy Student:3:4 Girl :master (7 Replies)
Discussion started by: invinzin21
7 Replies

4. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

5. Shell Programming and Scripting

To parse the line

Hi, I have a line QMNAME(qmgrname) STATUS(RUNNING) Can u jus tell me how to only get the status field ? And also the value of the status whether it is running or not running. -- Thanks (2 Replies)
Discussion started by: julie_s
2 Replies

6. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

7. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

8. Shell Programming and Scripting

please help to parse the line

cp4 0 0 170.217.86.10.1421 170.217.86.8.53308 ESTABLISHED tcp4 0 0 170.217.86.10.1421 170.217.86.8.62948 ESTABLISHED tcp4 0 0 170.217.86.10.1421 170.217.86.8.62949 ESTABLISHED tcp4 0 0 170.217.86.10.1421 ... (1 Reply)
Discussion started by: ajaya
1 Replies

9. Shell Programming and Scripting

please help to parse the line

cp4 0 0 170.217.86.10.1421 170.217.86.8.53308 ESTABLISHED tcp4 0 0 170.217.86.10.1421 170.217.86.8.62948 ESTABLISHED tcp4 0 0 170.217.86.10.1421 170.217.86.8.62949 ESTABLISHED tcp4 0 0 170.217.86.10.1421 ... (1 Reply)
Discussion started by: ajaya
1 Replies

10. Shell Programming and Scripting

How to parse a line?

I'm currenting trying to parse the out put of the following command. iostat -xtc -r |grep cmdk0 which produces the output cmdk0,0.2,0.0,1.2,0.0,0.0,0.0,39.7,0,0,0,0,0,0,0,99 I'm then trying to get the data to look like this: rw=0.2 ws=0.0 krs=1.2 kws=0.0 wait=0.0 actv=0.0... (2 Replies)
Discussion started by: edefoe
2 Replies
Login or Register to Ask a Question