How can I stop the unix script from trimming extra spaces?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I stop the unix script from trimming extra spaces?
# 1  
Old 10-27-2011
How can I stop the unix script from trimming extra spaces?

I have a file which contains certain records about users. the row length is always fixed to 205 characters.
Now I want to read each record line from the file, substring some portion out of it and put into another file. But I have observed that my script is trimming the extra spaces I have used for padding the records in order to keep the record lengths constant.
this is what I'm using :
Code:
while read line; 
    do  
        echo $line | cut -c6-12 >> $USER_NAME_FILE; 
    done < $FILE

the following input lines :
Code:
12345 Pramit 6789
25789   Amit 1234

give the following output:
Code:
 Pramit
 Amit 1

But I need only Amit in the second output. Smilie
Please help.



Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-27-2011 at 09:06 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-27-2011
If you need only the second column, go with field extractor ..
Code:
$ cut -d' ' -f2 < infile
Pramit
Amit

# 3  
Old 10-27-2011
Double-quote the variable (echo "$line").
# 4  
Old 10-27-2011
Or this:
Code:
cut -d' ' -f2 < infile > outfile

Based on jayan_jay's post!

I suggest you to use "awk" instead of "cut":
Code:
awk '{print $2}' infile
# Or
awk '{print substr($0, 7, 5)}' infile
# Or, maybe, you have an error with the positions in your cut
cut -c7-11 infile

I hope it helps.
# 5  
Old 11-02-2011
Thanks to everyone... your effort really saved my time. Smilie.. Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string causes extra spaces

Hello, I have an xml file and my aim is to grab each line in keywords file and search the string in another file. When keyword is found in xml file,I expect the script to go to previous line in the xml file and grab the string/value between two strings. It's almost working with an error. tab... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Trimming Spaces in Unix

Hi All, I am using following script to name the file base of some values #!/bin/sh sourcefile=$1 awk ' BEGIN{ n = 1; name = "FILEFILE12" n ".txt"; } { if (substr($0,1,10) == "FILEFILE12") { close (name) n++ a = substr($0,11,10); b = substr($0,21,5); name = b "_Src_" a ".txt" }... (6 Replies)
Discussion started by: manish8484
6 Replies

3. Shell Programming and Scripting

Trimming spaces from a variable

Hi guys, when I take substring of a particular data using this command var=substr($0,11,10) it comes with spaces, when I am trying to trim the spaces it is not allowing me to do that. Can you please help me out on that. As I have to reverse the output of the variable also. ---------- Post... (0 Replies)
Discussion started by: manish8484
0 Replies

4. Shell Programming and Scripting

Remove of extra spaces from the trailing

HI, I need the help from the experts like I have created one file with text like: a b c d e f g h i j k l So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (8 Replies)
Discussion started by: bhanudhingra
8 Replies

5. Shell Programming and Scripting

trimming out spaces in solaris

friends, I have a script in solaris 10 SPARC system which is like this date '+Time: %m/%d/%y %H:%M:%S' echo " GDBRR GDLRR GDBWR GDLWR GDRRR GDRWR " sar -b 10 10 |/usr/xpg4/bin/awk '!/Average/ && !/SunOS/ && !/bread/ {$1="";T=$2;T1=$3;T2=$5;T3=$6;T4=$8;T5=$9}{print(T"\t",... (1 Reply)
Discussion started by: achak01
1 Replies

6. Shell Programming and Scripting

csh script for deleting extra spaces in text file

I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Discussion started by: hertingm
2 Replies

7. UNIX for Advanced & Expert Users

Trimming the spaces

Hi, How can I remove the unwanted spaces in the line. 123456 789 ABC DEF. - I wanna remove the sapces in this line, I need the output 123456789ABCDEF. Pls help me...... (3 Replies)
Discussion started by: sharif
3 Replies

8. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

9. Solaris

spaces trimming while assigning to a variable

Hi my lovely friends, Im writing one pgm in which i trying to assign some values like $var='Jun 6' but if i do echo of this $var will trim the spaces expect one space. $echo $var $Jun 6 But if var='Jun 28', then this will works fine $echo $var $Jun 28 this is required to exctract... (2 Replies)
Discussion started by: Lokesha
2 Replies

10. UNIX for Dummies Questions & Answers

To remove the extra spaces in unix

Hi... I am quite new to Unix and would like an issue to be resolved. I have a file in the format below; 4,Reclaim,ECXTEST02,abc123,Harry Potter,5432 6730 0327 5469,0603,,MC,,1200,EUR,sho-001,,1,,,abc123,1223 I would like my output to be as follows; 4,Reclaim,ECXTEST02,abc123,Harry... (4 Replies)
Discussion started by: Sho
4 Replies
Login or Register to Ask a Question