Sed appending string using for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed appending string using for loop?
# 1  
Old 01-28-2007
Sed appending string using for loop?

Hi All,

I have been trying to format a file using sed. I can't seem to get the syntax right. I want to append the string from file1.txt to file1.csv with the final output file being file2.csv, but before the string is appended to the end of each line, first insert a comma.

Here is the sed command I'm running within the for loop:

for i in `cat file1.txt`
do sed 's/$/,'${i}'/' file1.csv > file2.csv
done


Below is an example of what I'm trying to accomplish:

file1.txt:
r2-1-config
r2-2-config
r2-3-config
r2-4-config
r2-5-config
r2-6-config
r2-7-config
r2-8-config
r2-9-config
r2-10-config
r2-11-config
r2-12-config
r2-13-config
r2-14-config
r2-15-config
r2-16-config
r2-17-config
r2-18-config
r2-19-config
r2-20-config
r2-21-config
r2-22-config
r2-23-config
r2-24-config
r2-25-config
r2-26-config
r2-27-config
r2-28-config
r2-29-config
r2-30-config


file1.csv:
r2-1,r2-ts,2033
r2-2,r2-ts,2034
r2-3,r2-ts,2035
r2-4,r2-ts,2036
r2-5,r2-ts,2037
r2-6,r2-ts,2038
r2-7,r2-ts,2039
r2-8,r2-ts,2040
r2-9,r2-ts,2041
r2-10,r2-ts,2042
r2-11,r2-ts,2043
r2-12,r2-ts,2044
r2-13,r2-ts,2045
r2-14,r2-ts,2046
r2-15,r2-ts,2047
r2-16,r2-ts,2048
r2-17,r2-ts,2049
r2-18,r2-ts,2050
r2-19,r2-ts,2051
r2-20,r2-ts,2052
r2-21,r2-ts,2053
r2-22,r2-ts,2054
r2-23,r2-ts,2055
r2-24,r2-ts,2056
r2-25,r2-ts,2057
r2-26,r2-ts,2058
r2-27,r2-ts,2059
r2-28,r2-ts,2060
r2-29,r2-ts,2061
r2-30,r2-ts,2062


file2.csv:
r2-1,r2-ts,2033,r2-1-config
r2-2,r2-ts,2034,r2-2-config
r2-3,r2-ts,2035,r2-3-config
r2-4,r2-ts,2036,r2-4-config
r2-5,r2-ts,2037,r2-5-config
r2-6,r2-ts,2038,r2-6-config
r2-7,r2-ts,2039,r2-7-config
r2-8,r2-ts,2040.r2-8-config
r2-9,r2-ts,2041,r2-9-config
r2-10,r2-ts,2042,r2-10-config
r2-11,r2-ts,2043,r2-11-config
r2-12,r2-ts,2044,r2-12-config
r2-13,r2-ts,2045,r2-13-config
r2-14,r2-ts,2046,r2-14-config
r2-15,r2-ts,2047,r2-15-config
r2-16,r2-ts,2048,r2-16-config
r2-17,r2-ts,2049,r2-17-config
r2-18,r2-ts,2050,r2-18-config
r2-19,r2-ts,2051,r2-19-config
r2-20,r2-ts,2052,r2-20-config
r2-21,r2-ts,2053,r2-21-config
r2-22,r2-ts,2054,r2-22-config
r2-23,r2-ts,2055,r2-23-config
r2-24,r2-ts,2056,r2-24-config
r2-25,r2-ts,2057,r2-25-config
r2-26,r2-ts,2058,r2-26-config
r2-27,r2-ts,2059,r2-27-config
r2-28,r2-ts,2060,r2-28-config
r2-29,r2-ts,2061,r2-29-config
r2-30,r2-ts,2062,r2-30-config

Last edited by cstovall; 01-28-2007 at 07:22 PM..
# 2  
Old 01-28-2007
Try...
Code:
paste -d, file1.csv file1.txt > file2.csv

# 3  
Old 01-28-2007
Thank you Ygor! The paste command and delimiter flag worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk appending values inside a for loop

Hi i have a 2 files say test1 and test2 with the following data. cat file test test1 i want to append the output from a awk one liner to both the files. for i in cat file;do awk '/ whats happening is its printing the output properly. but not appending the way it showing in print... (1 Reply)
Discussion started by: venkitesh
1 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

4. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

5. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

6. Shell Programming and Scripting

Need help appending a string to a field

Hi, This is driving me nuts, can't think of any easy way to do it. I need to append a string ".00" only in the third field of a file, and only if it does NOT have a decimal point already Here is what the file looks like- 1400030846,2,17,POL GENERAL 1400030900,3,14.95,FIC GENERAL If... (7 Replies)
Discussion started by: sfisk
7 Replies

7. Shell Programming and Scripting

appending two string is not working

Dear Memebers appending string is not working in loop outside of loop itw working fine, but when i put in loop its not working. please look into this code and suggest me how to do this. supplierCode.txt --------------- UGEN SLAND CEL here is my script ------------------- ... (11 Replies)
Discussion started by: krishna9
11 Replies

8. Shell Programming and Scripting

appending and sed

Hello, I want to add string #REAL at the end of all lines that contain real numbers. How to do this using sed ? (1 Reply)
Discussion started by: scotty_123
1 Replies

9. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies

10. Shell Programming and Scripting

Appending string to xml file using "sed"

Hi folks, Following a section in opmn.xml file: </process-type> <process-type id="OC4J_RiGHTv_IRD1" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question