How to pre-append a variable string to each line in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pre-append a variable string to each line in a file?
# 1  
Old 12-29-2013
How to pre-append a variable string to each line in a file?

How to pre-append a variable string to each line in a file contains both single and double quotes?

The variable string has no quotes in it.

thank you very much.

Smilie

Last edited by dtdt; 12-29-2013 at 12:02 PM..
# 2  
Old 12-29-2013
Quote:
Originally Posted by dtdt
which contains both single and double quotes in shell script?

thank you very much.

Smilie

Show us your input and expected output. we can't guess your "which contains" Smilie
# 3  
Old 12-29-2013
the original file contain quotes, not the pre-append variable string.

ty
------------------------
the acutal problem is: I have a set of files, file_a, file_b... file_xx, all those files have quotes inside somewhere

I need to pre-append a string

Axyz for all lines in file_a
Bcde for all lines in file_b
...
X-no-pattern-variable for all lines in file_xx

Last edited by dtdt; 12-29-2013 at 12:38 PM..
# 4  
Old 12-29-2013
Try something like:
Code:
awk 'FNR==1{f=FILENAME; sub(/.*_/,x,f); p=toupper(f f f)}{print p $0}' file_*

or to separate files:
Code:
awk 'FNR==1{f=FILENAME; t=f ".new"; sub(/.*_/,x,f); p=toupper(f f f)}{print p $0>t}' file_*

# 5  
Old 12-29-2013
i use a loop to bring in the pre-append variable

Quote:
Originally Posted by Scrutinizer
Try something like:
Code:
awk 'FNR==1{f=FILENAME; sub(/.*_/,x,f); p=toupper(f f f)}{print p $0}' file_*

or to separate files:
Code:
awk 'FNR==1{f=FILENAME; t=f ".new"; sub(/.*_/,x,f); p=toupper(f f f)}{print p $0>t}' file_*

# 6  
Old 12-29-2013
Me too (it is implicit in awk), I guessed it would be the last part of the filename. If that is not the case, could you show us your loop?
# 7  
Old 12-29-2013
i built a text file with pre-append string, and file name as:

Code:
axxdfd, f1
bxdfsdffdsfsdfs, f2
....
xxxdfsfs, f3

Code:
cat flist.txt | while read line; do
   pretext=`echo $line | cut -d',' -f1`
   filen=`echo  $line | cut -d',' -f2`

   #do the work
   #append each line in the file
done


Last edited by Scrutinizer; 12-29-2013 at 01:22 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

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

4. Shell Programming and Scripting

append to same string variable in loop

I want to append values to same string variable inside a recursive function that I have .. I do not want to write to any file but use a variable.. Can anyone please help with it? Thanks in advance. (6 Replies)
Discussion started by: Prev
6 Replies

5. Shell Programming and Scripting

Append string to first line of a file

Hi, Please suggest me to write unix command, HEADER20110101 string append to first line of a file.. Regards Akshu (3 Replies)
Discussion started by: akshu.agni
3 Replies

6. Shell Programming and Scripting

Append the end of each line in a file with a given string

Hi friends, I have a file containing many lines as follows. M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a I would like to append the string "\0" at the end of each line in the file. The output should look... (10 Replies)
Discussion started by: nmattam
10 Replies

7. Shell Programming and Scripting

Append a string at the end of every line in a file

Hi Friends, I have a file with many lines as shown below. /START SAMPLE LINE/ M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\port\Post.java@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\switchview\View.java@@\main\v6.4.0_pit_a /END SAMPLE LINE/ I... (1 Reply)
Discussion started by: nmattam
1 Replies

8. UNIX for Dummies Questions & Answers

Append line and variable at fixed postion in file in unix

Hi I have a input file ::: 1583904589034853904 1690234849023849023 159823890238409 1690238490238490238490 . . . The output file should have the record 16 appended to the record 15 and a variable should be added at a FIXED POSTION at 55. The records are been processed inside a loop... (3 Replies)
Discussion started by: akashhello
3 Replies

9. Shell Programming and Scripting

How can I append a string at the end of a line in a file

Hi, guys. I have one question: I have a file called "group", the contents of it is below: ******************************** ... test:x:203: sales:x:204: repair:x:205: research:x:206:brownj ... *********** Now I want to add string ",sherrys" at the end of "research:x:206:brownj", so... (5 Replies)
Discussion started by: daikeyang
5 Replies

10. Shell Programming and Scripting

line contains a string write to new file otherwise append

hi i have the following function, reading file line by line, if line contains "Change state" i need to cut some fields otherwise i need to append the total line to the new file function parse() { while read LINE do echo $LINE |grep -q "Change state" if then echo $LINE |grep -q... (1 Reply)
Discussion started by: Satyak
1 Replies
Login or Register to Ask a Question