Need to insert a comment string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to insert a comment string
# 1  
Old 11-01-2013
Need to insert a comment string

I need to insert a comment string to a couple of fields for every line in the file. Different comment string for each field.

In below file, fields 7 and 8



Code:
 
"R","1","t0000684","","10/31/2013","10/31/2013","","","","750.23"
"R","2","t0000614","","10/31/2013","10/31/2013","","","","700.23"

Any assistance greatly appreciated!

Thanks.
# 2  
Old 11-01-2013
And what would be desired output for that sample data?
# 3  
Old 11-01-2013
What does your input data look like? What tells it what data to put in what columns?

I'm assuming that input is like
Code:
7 asdf
8 abc

so I can do this:

Code:
awk 'NR==FNR { FIELD[FNR]=$1 ; DATA[FNR]=$2; next } { FS="\",\"" } { $(FIELD[FNR])=DATA[FNR] } 1' inputfile

# 4  
Old 11-01-2013
Insert text string in file

Quote:
Originally Posted by Corona688
What does your input data look like? What tells it what data to put in what columns?

I'm assuming that input is like
Code:
7 asdf
8 abc

so I can do this:

Code:
awk 'NR==FNR { FIELD[FNR]=$1 ; DATA[FNR]=$2; next } { FS="\",\"" } { $(FIELD[FNR])=DATA[FNR] } 1' inputfile


7 import
8 One Time

So the desired results in the file:

Code:
 
"R","1","cd00331","","10/31/2013","10/31/2013","import","One Time","","1100.66"
"R","2","cd00312","","10/31/2013","10/31/2013","import","One Time","","435.66"

Thanks!
# 5  
Old 11-01-2013
Oh, you want the same columns changed in every line. OK:

Code:
awk 'NR==FNR { FIELD[FNR]=$1 ; $1=""; DATA[FNR]=$0; next } { FS="\",\"" ; OFS=FS } { for(X in FIELD) { $(FIELD[X])=DATA[X] } } 1' replacefile inputfile > outputfile

# 6  
Old 11-01-2013
I'm afraid this doesn't quite do what was requested, at least not with my mawk 1.3.3 . Try a small modification:
Code:
awk     'FNR==NR        {TMP=$1 ; $1=""; DATA[TMP]=substr($0,2); next }
         FNR==1         {OFS=FS="\",\"" ; $0=$0}
                        {for(TMP in DATA) { $TMP=DATA[TMP] }
                        }
         1
        ' file2 file1
"R","1","t0000684","","10/31/2013","10/31/2013","import","One Time","","750.23"
"R","2","t0000614","","10/31/2013","10/31/2013","import","One Time","","700.23"

# 7  
Old 11-02-2013
Or maybe just a one-off solution like:
Code:
awk '{$7=c1; $8=c2}1' c1='"this is comment 1"' c2='"this is comment 2"' FS=, OFS=, file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. UNIX for Dummies Questions & Answers

Insert folder name in text string

Hi, I am trying to organize a badly constructed web server. Basically every file is in public_html which is horrendous. There's about 50 images and 20 html files (not counting the css files and pdf docs). I am trying to update each .html file using sed based on: Where string contains *.jpg... (9 Replies)
Discussion started by: enginama
9 Replies

3. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

4. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

5. UNIX for Dummies Questions & Answers

insert multiple characters in string

Hello, newb here :o How do I add square brackets before and after the first character in a string using sed? e.g. 0123456 123456 My attempts have been fruitless. sed 's/.\{0\}//' Thanks. (2 Replies)
Discussion started by: shadyuk
2 Replies

6. Shell Programming and Scripting

sed escape character for comment string "/*"

Good afternoon all, I'm hoping my newbie question can help bolster someone's street_cred.sh today. I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely). To do this, I'm essentially... (4 Replies)
Discussion started by: toeharp
4 Replies

7. Shell Programming and Scripting

How to insert a string in a file at specified place?

Hi all, I want to insert a string in a specified place of a very large file. I am giving an example of the task: I love football. Above is a sentence in a file and I want to insert a string "the" between love and football. It is not sure that where this particular line exists. It has to... (4 Replies)
Discussion started by: naw_deepak
4 Replies

8. Shell Programming and Scripting

fetch string and insert later

"are you billy_smith ?" replace with "are you billy_smith ? my name is billy_smith" how to fetch the name "billy_smith" and using it later I need sed script to do this, any one can help? Thanks (6 Replies)
Discussion started by: playinmel.com
6 Replies

9. UNIX for Dummies Questions & Answers

Can I use sed to insert a string which has colon

Hi, all, I wonder if I can use sed to insert a string which has a colon. I have a txt file a.txt like the following TRAIN/DR1/FCJF0/SI1027.MFC TRAIN/DR1/FCJF0/SI1657.MFC I want to insert a string C:/TIMIT/TIMIT at the begining of each line. I use the commond: TIM=C\:/TIMIT/TIMIT... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies

10. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies
Login or Register to Ask a Question