I don't want to truncate trailing spaces and ^M at the end of line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I don't want to truncate trailing spaces and ^M at the end of line
# 1  
Old 12-17-2009
I don't want to truncate trailing spaces and ^M at the end of line

I have a script wherein I access each line of the file using a FOR loop and then perform some operations in each line. The problem is each line that gets extracted in FOR loop truncates trailing blank spaces and control characters (^M) that is present at the end of each line. I don't wan this to happen.

Can anyone help me with this at the earliest? I'm almost done with the code except for this bit

Here is a part of the code:
Code:
for i in `cat $c`
do
#IFS=" "
j=`echo $i | cut -c 1-2`
if [ $j -eq 02 ]
then
e=`echo $i | cut -c 27-34`
f=`expr $e + $m`
g=$(printf "%.8d" "$f")
echo $i | \
sed -e "s/$e/$g/" >> tesfile
elif [ $j -eq 01 ]
then
h=`echo $i | cut -c 28-35`
k=`expr $h + $m`
l=$(printf "%.8d" "$k")
echo $i | \
sed -e "s/$h/$l/" >> tesfile
else
echo $i >> tesfile
fi


Last edited by Scott; 12-17-2009 at 05:53 AM.. Reason: Please use code tags
# 2  
Old 12-17-2009
you're losing the spaces early on...probably better to use awk for this...

Can you provide some sample input, rules and expected output ?
# 3  
Old 12-17-2009
Thanks for your help!

Here are the details that you requested

Sample lines from input file:
002009101307350520093320091013335 ^M
0102654N3FKAA20091013S0013200000001 ^M
0233L07A01051624843060000100000001 ^M
0233L07A02050869301010000200000001 ^M
.......

Expected output in new file:
002009101307350520093320091013335 ^M
0102655N3FKAA20091013S0013200000006 ^M
0233L07A01051624843060000100000006 ^M
0233L07A02050869301010000200000006 ^M
.......

Actual Output in new file:
002009101307350520093320091013335
^M
0102655N3FKAA20091013S0013200000006
^M
0233L07A01051624843060000100000006
^M
0233L07A02050869301010000200000006
^M
.......

I'm actually using one file to create multiple files with changes in few fields of the file

Can you let me know the awk command that can be used with this? I tried searching in google but couldn't find. I'm still in the process of searching

Please let me know if you require any further details

Thanks a lot!
Shobana
# 4  
Old 12-17-2009
what are the rules you're applying ?

and what is the value of m ? I don't see that defined in your snippet ?
# 5  
Old 12-17-2009
Please find below the entire code

Code:
count=0
b=0
val=0
d=1
e=0
m=5
c=GR-PPDS_0335_02654_20091013_074653.EAI
while [ $count -lt 3 ]
do
b=`echo $c | cut -d"_" -f3`
b=`expr $b + 1`
val=$(printf "%.5d" "$b")
c=`echo "$d"GR-PPDS_0335_"$val"_20091013_074653.EAI`
cp GR-PPDS_0335_02654_20091013_074653.EAI $c
  cat $c | \
    sed -e "s/2654/$b/" \
    1>>$_outfile 2>>$logfile 0>>correctfile
mv correctfile $c
#While IFS='\n'
#do
for i in `cat $c`
do
#IFS=" "
j=`echo $i | cut -c 1-2`
if [ $j -eq 02 ]
then
e=`echo $i | cut -c 27-34`
f=`expr $e + $m`
g=$(printf "%.8d" "$f")
  echo $i | \
    sed -e "s/$e/$g/" >> tesfile
elif [ $j -eq 01 ]
then
h=`echo $i | cut -c 28-35`
k=`expr $h + $m`
l=$(printf "%.8d" "$k")
  echo $i | \
    sed -e "s/$h/$l/" >> tesfile
else
echo $i >> tesfile
fi
#IFS=" "
#done

Thanks,
Shobana

---------- Post updated at 08:34 AM ---------- Previous update was at 07:49 AM ----------

Can anyone help me out with this problem?

Thanks,
Shobana
# 6  
Old 12-17-2009
I see that you have tried with setting IFS to different values. However
Code:
IFS='\n'

should be
Code:
IFS='
'

With that, my test on Linux gave the right results.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove or truncate trailing nulls from file

I want to remove from a file the trailing null characters (0x00) and stop doing so when a different byte is found (which should not be deleted), and either put the result to the same file or a different one. Any ideas? (9 Replies)
Discussion started by: Tribe
9 Replies

2. UNIX for Dummies Questions & Answers

delete trailing whitespace from end of each line in column 1 only

Hi All. How can I convert this: ABC_1_1 ABC_1_2 ABC_1_3 into this: ABC_1 1 ABC_1 2 ABC_1 3 I tried this command but it is not working: awk '{sub(/+$/,"\t", $1)}{print}' Any suggestions on how to fix this? Thank you :wall: Please use code tags when posting data and... (3 Replies)
Discussion started by: danieladna
3 Replies

3. Shell Programming and Scripting

./TRUNCATE-PS_TXN.sh: line 54: syntax error: unexpected end of file

Hi All , My below script is for chacking vaule and then trucating table : ___________ test4@aceuatcs04:/u01/test4/SOLID/Testscript>cat TRUNCATE-PS_TXN.sh #-------------------------------------------------------------------- # Created by:Kaushlesh Yadav # Generated on: 15/07/2010 # Job... (4 Replies)
Discussion started by: kaushelsh168
4 Replies

4. Shell Programming and Scripting

append end of line with 8 spaces

child_amt=$amount prev_line="$prev_line $child_amt" i am getting the result like this 21234567890001343 000001004OLFXXX029100020091112 0000060 but i want 8 spaces between the eg: 21234567890001343 000001004OLFXXX029100020091112 0000060 how can i do this in .ksh (1 Reply)
Discussion started by: kshuser
1 Replies

5. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

6. Shell Programming and Scripting

remove trailing spaces from a line

I want to remove the trailing spaces from any line of file. line ending does not follow any pattern. plz help (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

7. Shell Programming and Scripting

I don't want to truncate spaces

I have a script that is adding text to a existing file and creating new file. This part is working fine.... But the new file is truncated by script and all null spaced column are truncated. What is my solution...please some body advice .... thanks in advance pre_line="+_)(*&" spaces=" "... (2 Replies)
Discussion started by: naveenkcl
2 Replies

8. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

9. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question