remove trailing newline characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove trailing newline characters
# 1  
Old 06-05-2007
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.... Smilie

Thanks and Regards,
SD
# 2  
Old 06-05-2007
Perhaps
Code:
while read name
do
  grep -A 2 $name somexml.xml
done < name.txt

# 3  
Old 06-05-2007
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  
Old 06-05-2007
Can you post a few lines from the output of
Code:
od -c names.txt

# 5  
Old 06-05-2007
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  
Old 06-05-2007
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  
Old 06-05-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

2. UNIX for Dummies Questions & Answers

Removing trailing x'0A' characters.

I am trying to remove trailing carriage return (x'0a') from a source program. What is a good way to do this for the whole file? TIA (4 Replies)
Discussion started by: wbport
4 Replies

3. UNIX for Dummies Questions & Answers

Removing trailing characters

I have been given a shell script that I need to amend. To do the following extract the filename from the flag file by removing the .flag extension. # Local variables # Find if the flag files exists MASK=coda_mil2*.flag # Are there any files? bookmark="40" fileFound=0 ls -1... (3 Replies)
Discussion started by: andymay
3 Replies

4. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

5. Shell Programming and Scripting

Remove trailing 0 from the field

Hi Freinds, I have file1.txt as below file1.txt 1521894~~-0.400~201207 1521794~~-0.486~201207 152494~~-0.490~201207 152154894~~-0.490~201207 1521894354~~-0.489~201207 expected output : 1521894~~-0.4~201207 1521794~~-0.486~201207 152494~~-0.49~201207... (9 Replies)
Discussion started by: i150371485
9 Replies

6. Shell Programming and Scripting

Remove trailing zeros

Hi I have a simple request but can't find the answer. I want to remove trailing zeros, and in some cases the fullstops, from the input data. Example of input file: FR002_15.000_20.000 SD475_5.000_10.500 FG5647_12.250_15.500 BH2463_30.555_32.000 Desired output file would be: ... (10 Replies)
Discussion started by: theflamingmoe
10 Replies

7. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

8. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

9. Shell Programming and Scripting

Remove trailing G

Hello, I am trying to write a script that will calculate the amount of data remaining in a storage volume. I'm running Tru64 Unix version 5.1B patch kit 6. The script is being run against an AdvFS domain. I am programming in Korn Shell version M-11/16/88f. The basic idea is that I want to run df... (3 Replies)
Discussion started by: Heathe_Kyle
3 Replies

10. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies
Login or Register to Ask a Question