Append string in all rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append string in all rows
# 1  
Old 11-30-2006
Append string in all rows

I would like to append a string at the end of each row of a .dat file.
please give me some advice? Thanks.

e.g.
#content in abc.dat
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
...


#after running the script/command, "bbbbbbbbbb" will be appended as follow.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbb
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbb
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbb
...
# 2  
Old 11-30-2006
It is just me or does this look like an exam question?
# 3  
Old 12-01-2006
I have writen a simple script to arhieve my purpose. However, "There is not enough memory available now" is prompted. Can my script be optimized to reduce resource? Thanks.

append_string=`cat ${source_file} | awk ' BEGIN { FS = "\n" } { if ($1 ~ "SWIFT5" ) {print $1, " :: SEND = N :: SEV = 0 :: ERROR = 11414 :: COMP = Name :: "
} else {print $1;} }'`
# 4  
Old 12-01-2006
Here is a sed solution.

Code:
str="string to be attached"
sed -e "s/.*/&$str/g" input.txt

# 5  
Old 12-01-2006
A Python alternative
Code:
#!/usr/bin/python
string_to_attach = "a string to attach"
for line in open("yourfile"):
    line = line.strip()
    print "%s%s" %( line, string_to_attach )

prompt:
Code:
# python test.py > outfile

# 6  
Old 12-01-2006
For the sed solution,


The Pattern is constructed with a variable string inside a text.
e.g.
aaa123aaa
aaa4 5aaa
aaa678aaa

sed -e "s/aaa*aaa/&$str/g" ${source_file}

Why I can't use "*" to find the pattern?
# 7  
Old 12-01-2006
Quote:
Originally Posted by Rock
For the sed solution,


The Pattern is constructed with a variable string inside a text.
e.g.
aaa123aaa
aaa4 5aaa
aaa678aaa

sed -e "s/aaa*aaa/&$str/g" ${source_file}

Why I can't use "*" to find the pattern?
I didnt quite get what you are looking for.

str should contain the string you want to append.

Does this help ?


Code:
sed -e "s/aaa.*aaa/&$str/g" ${source_file}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts Need help... I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp e.g. Header row1 row2 row3 Trailer The script should read all the files under... (3 Replies)
Discussion started by: phani333
3 Replies

2. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

3. Shell Programming and Scripting

How to append string checking other same string?

Hi , I have a file likeA-0044150|ABC/Frito/ A-0044150|GFHU A-0150075|Bud Racing A-0187811|Bud Light A-0187811|RW&B signmaking I Want the o/p likeA-0044150|ABC/Frito/,GFHU A-0150075|Bud Racing A-0187811|Bud Light,RW&B signmaking (2 Replies)
Discussion started by: jagdishrout
2 Replies

4. Shell Programming and Scripting

How to append a string by comparing another string?

Hi , I have one file like BUD,BDL BUDCAR BUD,BDL BUDLAMP ABC,CDF,KLT ABISKAR ABC,CDF,KLT CORNEL ABC,CDF,KLT KANNAD JKL,HNM,KTY,KJY JAGAN JKL,HNM,KTY,KJY HOUSE JKL,HNM,KTY,KJY KATAK JKL,HNM,KTY,KJY KOLKA The o/p should be like BUD,BDL BUDCAR,BUDLAMP ABC,CDF,KLT... (4 Replies)
Discussion started by: jagdishrout
4 Replies

5. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

6. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

7. Shell Programming and Scripting

How to append filename to rows in gzip stdout

Assume I have 2 gz files with 2 lines each as below a.gz a1,a a2,a b.gz b1,b b2,b If I issue gzip -d -c *.gz I will get below in the stdout a1,a a2,a b1,b b2,b What I need it the filename appended to each row either at the front or back. a.gz,a1,a a.gz,a2,a (2 Replies)
Discussion started by: ericlim
2 Replies

8. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

9. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies

10. Shell Programming and Scripting

How to append a Value to all the rows in a file

Hi, We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on. For Example Incoming file 1 ... (2 Replies)
Discussion started by: dsshishya
2 Replies
Login or Register to Ask a Question