The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Remove Trailing spaces after a delimiter kiran_418 UNIX for Dummies Questions & Answers 1 04-29-2008 02:19 PM
Remove trailing G Heathe_Kyle Shell Programming and Scripting 3 04-14-2008 10:56 AM
how to keep newline characters in command execution result? pankai Shell Programming and Scripting 2 01-02-2008 05:41 PM
How to remove trailing spaces mahek_bedi UNIX for Dummies Questions & Answers 2 08-10-2007 07:21 AM
Formatting a text file based on newline and delimiter characters ntekupal Shell Programming and Scripting 5 05-11-2007 03:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-05-2007
shweta_d shweta_d is offline
Registered User
  
 

Join Date: May 2007
Posts: 11
remove trailing newline characters

Hello ,

I have the folowing scenario :
I have a text file as follows : (say name.txt)
ABC
DEF
XYZ

And I have one more xml file as follows : (say somexml.xml)
<Name>ABC</Name>
<Age>12</Age>
<Class>D</Class>
<Name>XYZ</Name>
<Age>12</Age>
<Class>D</Class>
<Name>DEF</Name>
<Age>12</Age>
<Class>D</Class>

I use each name from text , grep accordingly and extract three tags name,age,class from xml ..

The extraction works fine and so does grep ...but i want to move that in a loop ... i.e. as follows :

for each name in text file
do something ....(grep in xml)

i use the following command :
cat name.txt | while read someLine
set ${someLine}
name=$someLine
grep "$name" somexml.xml

but the problem here occurs during readline coz it reads a newline alongwith the name due to which it cannot grep further in xml .....
So basically i want to remove the trailing newline character.....

(I would kindly acknowledge even better and innovative ways of reading the file)

Hope my problem is clear....

Thanks and Regards,
SD
  #2 (permalink)  
Old 06-05-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Perhaps
Code:
while read name
do
  grep -A 2 $name somexml.xml
done < name.txt
  #3 (permalink)  
Old 06-05-2007
shweta_d shweta_d is offline
Registered User
  
 

Join Date: May 2007
Posts: 11
well... it still could not grep the "name" since it got a newline appended to it while using readLine ...but can you tell me what -A 2 on a grep do?
  #4 (permalink)  
Old 06-05-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Can you post a few lines from the output of
Code:
od -c names.txt
  #5 (permalink)  
Old 06-05-2007
shweta_d shweta_d is offline
Registered User
  
 

Join Date: May 2007
Posts: 11
any explanation of hat the abov does???coz i have already written the code for grep and extraction .
All i require to so is as described earlier.....
Regards,
SD
  #6 (permalink)  
Old 06-05-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by shweta_d
any explanation of hat the abov does???coz i have already written the code for grep and extraction .
All i require to so is as described earlier.....
Regards,
SD
Care to look into the man pages of grep ? Perhaps your version of grep does not have this flag ?

Code:
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places
              a line containing -- between contiguous groups of matches.
  #7 (permalink)  
Old 06-05-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Quote:
Originally Posted by shweta_d
any explanation of hat the abov does???coz i have already written the code for grep and extraction .
All i require to so is as described earlier.....
Regards,
SD
The od command display the content of the file, nongraphic characters are in form of C-language escape sequences.
That command will permit us to determine if your problem comes from your data file.

An example:
Code:
$ od -c datas.txt
0000000    E   s   c   a   p   e     033       a   n   d       C   R    
0000020   \r        i   n   s   i   d   e  \n
0000031
$
Jean-Pierre.
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 02: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