Splitting a variable based on newline char


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a variable based on newline char
# 1  
Old 03-26-2009
Power Splitting a variable based on newline char

Heeloo all,

A weird problem perhaps. May god save others from this problem.
I want to print each line from a variable.. the example below should make it clear.

Code:
smvar="Hello World1
Hello world 2
forgot there I guess"

for eachline in $smvar
           echo $eachline
end

Whats for my rescue? Smilie
# 2  
Old 03-26-2009
Sily me..

Sorry bout that one, I got the solution. Its quite simple actually.. so here we go..

Code:
smvar="Hello World1
Hello world 2
forgot there I guess"

echo "$smvar" | while read each
do
  echo "$each"
done

# 3  
Old 03-26-2009
Quote:
Originally Posted by pavanlimo
Heeloo all,

A weird problem perhaps. May god save others from this problem.
I want to print each line from a variable.. the example below should make it clear.

Code:
smvar="Hello World1
Hello world 2
forgot there I guess"

for eachline in $smvar
           echo $eachline
end

Whats for my rescue? Smilie
Is it each line from a variable or each variable from one line ? Smilie
# 4  
Old 03-26-2009
Its each line from a variable..
Looks like confused some!. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace the unexpected newline char with space in a Fixed width file

Input eg: Ouput Expected. The #rd line had the unexpted new line, which need to be replaced with space. I was planing to go with checking the length of each line using awk and if the length is less than the defeined limit, (12 in above case) will replace the newline with space. ... (5 Replies)
Discussion started by: deepakwins
5 Replies

2. UNIX for Dummies Questions & Answers

Remove newline char from variable

I have a file ABC.DAT with 2 columns avaialble Data format : XYZ!$#$!120 XXZ!$#$!1000 YYZ!$#$!104 While running the following code : FILE_COUNTER=1; RECORD_CN_FILE_COUNT=$((`wc -l ABC.DAT| cut -f1 -d' '`)); while do FILE_NAME=`cat ABC.DAT.DAT| head -$FILE_COUNTER |tail -1 | awk -F... (1 Reply)
Discussion started by: Nikhil Gautam
1 Replies

3. Shell Programming and Scripting

Shell to remove a newline char from selected rows in a file.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (3 Replies)
Discussion started by: mailme0205
3 Replies

4. UNIX for Dummies Questions & Answers

Remove a newline char from selected rows.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (2 Replies)
Discussion started by: mailme0205
2 Replies

5. Shell Programming and Scripting

cutting long text by special char around 100 byte and newline

Regard, How can i cut the text by special char(|) around 100 byte and write the other of the text at newline using Perl. ... (3 Replies)
Discussion started by: Shawn, Lee
3 Replies

6. Shell Programming and Scripting

Help substituting text in a file having a single line but no newline char

Hello, Need help substituting a particular word in a file having a single line but no newline character at the end. I was trying to use sed but it doesn't work probably because there is no newline char at the end of the line. $ cat hlq_detail /outputs/alvan23/PDFs/bills $ cat... (5 Replies)
Discussion started by: Shan_u2005
5 Replies

7. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

8. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies

9. UNIX for Dummies Questions & Answers

Search based on 1st char of a row.

I want to search for the very first character in a row from the file. It is very similar to the way we use Mainframes File-Aid, quick search option. I couldnt find anything helpful with the grep command. Does any one has an idea, how to perform this? (6 Replies)
Discussion started by: videsh77
6 Replies
Login or Register to Ask a Question