Add a new field at the end of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a new field at the end of each line
# 1  
Old 12-11-2013
Add a new field at the end of each line

i want to add a white-space at the end of each line for my inp.file, but when i do it, the result is a new line with a white-space between each line!

my input:
Code:
 
 2012 0811 1223 15.2 L  38.393  46.806  9.0  Teh 78 0.5 6.5LTeh                1 
 GAP=74                                                                        E 
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6 
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7 
 MHD  SN E    0   1225 41.58    278118.0  0.8                           0.0   0 
 VIS  SE E    0   1225 35.81    916098.0 1.28                           429 179 
 KFM  BZ E    0   1226 23.74       -99.0 3.64                           548 170 
 GLO  SZ E    0   1226 18.91     92061.7  0.8                           656 107 
 RAZ  SN E    0   1225 34.17    127538.0 0.68                           0.0   0 
 GZV  SE E    0   1225 23.49    448179.0  0.6                           0.0   0 
 GZV  SZ E    0   1225  9.53    271464.0 0.68                           375 125

my awk script:
Code:
awk '{print $0," "}' in > out

result:
Code:
   
 2012 0811 1223 15.2 L  38.393  46.806  9.0  Teh 78 0.5 6.5LTeh                1
   
 GAP=74                                                                        E  
 
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6  
 
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7  
 
 MHD  SN E    0   1225 41.58    278118.0  0.8                           0.0   0  
 
 VIS  SE E    0   1225 35.81    916098.0 1.28                           429 179  
 
 KFM  BZ E    0   1226 23.74       -99.0 3.64                           548 170 
  
 GLO  SZ E    0   1226 18.91     92061.7  0.8                           656 107 
 
 RAZ  SN E    0   1225 34.17    127538.0 0.68                           0.0   0 
 
 GZV  SE E    0   1225 23.49    448179.0  0.6                           0.0   0 
 
 GZV  SZ E    0   1225  9.53    271464.0 0.68                           375 125

anybody knows about this problem? i used SL Linux V6.3 32bit
# 2  
Old 12-11-2013
use tr and then try awk something like this

Code:
$ tr -s '\n'  <file | awk '$0=$0" "'

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 12-11-2013
unfortunately "tr" does not work on my OS!
# 4  
Old 12-11-2013
Hi,
you can try as here:
Code:
$ cat -A inp.file
 $
 2012 0811 1223 15.2 L  38.393  46.806  9.0  Teh 78 0.5 6.5LTeh                1 $
 GAP=74                                                                        E $
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6 $
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7 $
 MHD  SN E    0   1225 41.58    278118.0  0.8                           0.0   0 $
 VIS  SE E    0   1225 35.81    916098.0 1.28                           429 179 $
 KFM  BZ E    0   1226 23.74       -99.0 3.64                           548 170 $
 GLO  SZ E    0   1226 18.91     92061.7  0.8                           656 107 $
 RAZ  SN E    0   1225 34.17    127538.0 0.68                           0.0   0 $
 GZV  SE E    0   1225 23.49    448179.0  0.6                           0.0   0 $
 GZV  SZ E    0   1225  9.53    271464.0 0.68                           375 125$

Code:
$ awk '{printf("%s \n", $0)}' inp.file | cat -A
  $
 2012 0811 1223 15.2 L  38.393  46.806  9.0  Teh 78 0.5 6.5LTeh                1  $
 GAP=74                                                                        E  $
 Iranian Seismological Center, Institute of Geophysics, University of Tehran   6  $
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO SNR AR TRES  W DIS CAZ7  $
 MHD  SN E    0   1225 41.58    278118.0  0.8                           0.0   0  $
 VIS  SE E    0   1225 35.81    916098.0 1.28                           429 179  $
 KFM  BZ E    0   1226 23.74       -99.0 3.64                           548 170  $
 GLO  SZ E    0   1226 18.91     92061.7  0.8                           656 107  $
 RAZ  SN E    0   1225 34.17    127538.0 0.68                           0.0   0  $
 GZV  SE E    0   1225 23.49    448179.0  0.6                           0.0   0  $
 GZV  SZ E    0   1225  9.53    271464.0 0.68                           375 125 $

option -A from command cat, show end line and no printable characters...
Regards
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 12-11-2013
Theres no reason your awk script
Code:
awk '{print $0," "}' in > out

should not work. I see that your lines are 80 char long. Is it possible there's line wrapping occurring in your terminal/editor?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-12-2013
Yes you right, i tested it on a Mint Linux and it works!
Thank you guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

How to add a character at end of line?

Hai, I have got a small requirement in my script. and i am using bash shell. I need to add a dot (.) for some particular line in a file. Say for example, $Cat rmfile 1 This is line1 2 This is line2 3 This is line3 O/p should be : $Cat rmfile 1 This is line1 2 This is line2. #... (2 Replies)
Discussion started by: Sivajee
2 Replies

3. Shell Programming and Scripting

Add line at the end

How to add a comma at the end of each line in this file?30 1412 30 3352 30 5254 30 5543 30 7478 3 28 3 30 3 39 3 54 3 108 3 152 3 178 3 182 3 214 3 271 3 286 3 300 3 348 3 349 3 371 (3 Replies)
Discussion started by: gunjan
3 Replies

4. Shell Programming and Scripting

Use sed to add comma to end of first field

Example data Gi1/10 Gi1/12 xl32lytscb07 3/11 to nyc 3/12 41764 ecomm 3/13 hxcsxsa 2/1 3/14 ziim570-rsvd 3/15 xl3NDSADM Po1 VPC trunk to xl3-i Po2 ***DO NOT ENABLE** Po13 *** VPC link to Po101 Po102 xl3-2lyg1accsgh-fe... (7 Replies)
Discussion started by: sumguy
7 Replies

5. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

6. Shell Programming and Scripting

how to add ; at the end of last line

hi, i have file which is having large sql query eg : i am executing this sql file but now i want to add ; after query on same line i.e. i should look like any idea how to achieve it ? (6 Replies)
Discussion started by: crackthehit007
6 Replies

7. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

8. UNIX for Advanced & Expert Users

Add line numbers to end of each line

Hi i would like to add line numbers to end of each line in a file. I am able to do it in the front of each line using sed, but not able to add at the end of the file. Can anyone suggest The following code adds line number to start of each line sed = filename | sed 'N;s/\n/\t/' how can i... (5 Replies)
Discussion started by: rudoraj
5 Replies

9. Shell Programming and Scripting

Add a new end of line

Hi, Does anyone know if its possible to add something like an end of line like c or java in unix? dirs=/home/nosnam var='' for dir in $dirs do listDirs=`ls -d1 $dir/*` for eachList in $listDirs do listRepos=`du -ks $eachList | awk '{ x+=$1 }; END { print x... (4 Replies)
Discussion started by: nosnam
4 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question