The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add white space to the end of a line with sed karlanderson Shell Programming and Scripting 7 03-09-2009 06:20 AM
How to trim the white space around a string in C program hxm1303 High Level Programming 18 10-17-2008 11:43 AM
stripping white space... Zak Shell Programming and Scripting 7 10-09-2007 08:41 AM
How to get just the word and clean the white space? paulofp Shell Programming and Scripting 2 06-22-2007 07:28 PM
How to keep white space is being deleted using read keelba Shell Programming and Scripting 1 05-30-2002 03:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2008
pondlife pondlife is offline
Registered User
  
 

Join Date: May 2004
Location: UK
Posts: 85
Question how to protect white space in for loop

Hi All,

I know there's a really simple answer to this but I just can't think of it

I'm processing a file which has lines containing white space i.e.

Quote:
this is the first line
this is the second line
this is the third line
And I want to perform some awk on each line but when I do the following:


Code:
for US in $( cat /tmp/unique-strings.tmp | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' | sed 's/\//\\\//g' )
do

echo ""
echo "$US"
echo ""

done

Each line gets broken down into it's component parts like this:

Quote:
this

is

the

first

line
How can I get it to echo each complete line? (In my code I obviously want to do more than just echo the line...)

Thanks,

p.
  #2 (permalink)  
Old 07-01-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,313
Sorry I don't get it. Please just show the input file and how the output should look like. If you work each element with for from a list, every element will be echoed line by line, that's as it is. In the example you use sed, not awk
  #3 (permalink)  
Old 07-01-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,346
Something like this?


Code:
awk '{for(i=1;i<=NF;i++){printf("%s\n\n", $i)}}' file

Regards
  #4 (permalink)  
Old 07-01-2008
pondlife pondlife is offline
Registered User
  
 

Join Date: May 2004
Location: UK
Posts: 85
Sorry, I'm not explaining myself clearly.

I would like the entire sentence to be treated as one string but it's getting split into component parts (each word).

Is there a way I can stop this split occuring?
  #5 (permalink)  
Old 07-01-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,313
Don't beat me, but you could just "cat" the file and everything will be fine hehe
But I guess you will do something with every sentence... I would prefer this

Code:
cat infile |\
while read LINE; do
    echo "This is my line:" ${LINE}
    #now do something with it in here
done

If you are going to parse the contents of the line, better use awk or sed like Franklin52 did.
  #6 (permalink)  
Old 07-01-2008
pondlife pondlife is offline
Registered User
  
 

Join Date: May 2004
Location: UK
Posts: 85
Quote:
Originally Posted by zaxxon View Post
Don't beat me, but you could just "cat" the file and everything will be fine hehe
But I guess you will do something with every sentence... I would prefer this

Code:
cat infile |\
while read LINE; do
    echo "This is my line:" ${LINE}
    #now do something with it in here
done

If you are going to parse the contents of the line, better use awk or sed like Franklin52 did.
Brilliant that's what I needed thanks

Believe it or not it's the first time I've used a while statement in one of my scripts!
  #7 (permalink)  
Old 07-01-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,811

Code:
while read record
do
echo "$record" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' | sed 's/\//\\\//g'| read US
echo ""
echo "$US"
echo ""

done < /tmp/unique-strings.tmp

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0