Add to constant fields at the end of every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add to constant fields at the end of every line
# 1  
Old 03-26-2010
Add to constant fields at the end of every line

Hi,
I want to add two fields with values '1000' and 'XYZ-1234' at the end of every line in a comma delimited file.
Should I use any command in a loop to add the fields or using any single command Shall I acheive it?
Kindly help me in code.

Thanks,
Poova.
# 2  
Old 03-26-2010
Code:
sed 's/$/1000,XYZ-1234/' file

# 3  
Old 03-26-2010
Small Change :

Code:
sed 's/$/,1000,XYZ-1234/' file


Last edited by Franklin52; 03-26-2010 at 05:33 AM.. Reason: Please use code tags, thank you!
# 4  
Old 03-26-2010
Thanks all... It's working. I was supposed to pass variable for 1000 and XYZ-1234 there. So I changed the command to ,
Code:
sed "s/$/,$id1,$id2/" file

When using single quote it was not working.

Last edited by Franklin52; 03-26-2010 at 11:50 AM.. Reason: Please use code tags!
# 5  
Old 03-26-2010
Try this:
Code:
sed 's/$/,'$1','$2'/' ifile


Last edited by Franklin52; 03-26-2010 at 11:50 AM.. Reason: Please use code tags!
# 6  
Old 03-26-2010
Quote:
Originally Posted by poova
Thanks all... It's working. I was supposed to pass variable for 1000 and XYZ-1234 there. So I changed the command to ,
sed "s/$/,$id1,$id2/" file

When using single quote it was not working.
If the replacement text in your variables contains characters or sequences which are special to sed, this approach will not work. Examples of such characters/sequences include '&', '/' (if default regular expression delimiters are being used), '\' followed by anything (it's either a defined escape sequence or an undefined sequence which may result in the loss of a backslash in your replacement text).

Assuming that your two values are stored in the shell variables $a and $b:
Code:
a='your 1st value'
b='your 2nd value'

The following are safe alternatives, one is pure shell (printf is usually a built-in these days) and the other is AWK:
Code:
while IFS='' read -r line; do printf '%s,%s,%s\n' "$line" "$a" "$b"; done < data

awk '{printf("%s,%s,%s", $0, a, b RS)}' a="$a" b="$b" file

Regards,
Alister

Last edited by alister; 03-26-2010 at 12:02 PM..
# 7  
Old 03-27-2010
Thanks a lot Alister. It's been more useful comment for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add text to the end of line

Seems simple but ive been searching for a good hour of so I have a text file and would like to add a string to the end of line 5 ( as an example) to ake tings hard the line number we have to add the text to is stored in a variable cunningly name $Line_to_append any ideas on how this could... (2 Replies)
Discussion started by: dunryc
2 Replies

2. Shell Programming and Scripting

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: 2012 0811 1223 15.2 L 38.393 46.806 9.0 Teh 78 0.5 6.5LTeh 1 GAP=74 ... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

3. Shell Programming and Scripting

Counting fields backwards from end of line

Hello, file.dat 1,2,3,4,5,6,7,8 1,2,3,4,5,6 1,2,3,4 I need to output the value in the third column from the end: 6 4 2 Thanks! (2 Replies)
Discussion started by: palex
2 Replies

4. 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

5. Shell Programming and Scripting

Add string end of line

Hello How can I add a string (always the same) at the end of a specific line in a file... The file is: 000000001 041 L $$aspa 000000001 088 L $$aJ.E.N. 551 000000001 090 L $$aINFORMES JEN 000000001 100 L $$aautor 1 ---- 000000002 041 L $$aeng 000000002 088 L $$aJ.E.N. 1... (13 Replies)
Discussion started by: ldiaz2106
13 Replies

6. 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

7. 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

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

Append tabs at the end of each line in NAWK -- varying fields

Hi, I need some help in knowing how I can append tabs at the end of each line... The data looks something like this: field1, field2, field3, field4 1 2 3 4 5 I have values in field1 and field 2 in the first row and I would like to append tab on field3 and field4 for the first row..and in... (6 Replies)
Discussion started by: madhunk
6 Replies

10. 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
Login or Register to Ask a Question