Computing.Net > Forums > Unix > How to insert string in a list?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to insert string in a list?

Reply to Message Icon

Name: lamos
Date: September 19, 2005 at 16:18:32 Pacific
OS: Solaris
CPU/Ram: 2GB
Comment:

Hi Folks,

Is there a way to insert a string of text in a database list by first locating a specific word and then inserting (or appending) a string every 3rd line after it. I've tried it with sed with limited success. The "word appears in several blocks of text; I wish to insert the string in each block on the 3rd line.

Thanks for your help in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 19, 2005 at 20:19:20 Pacific
Reply:

How are you defining a "block" of text? Do you want to modify only every 3rd line?

You might want to submit a sample file.


0

Response Number 2
Name: lamos
Date: September 20, 2005 at 07:26:14 Pacific
Reply:

Hi Nils,

Yes, here's an example. I'm trying to locate the word "Word1" for each block as such:


Word1 AAAAAAAAAAAAAA {
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC
DDDDDDDDDDDDDD
}


Word1 BBBBBBBBBBBBB {
ZZZZZZZZZZZZZ
WWWWWWWWWWWWW
QQQQQQQQQQQQ
}


Word12 AAAAAAAAAAAAA {
BBBBBBBBBBBBB
CCCCCCCCCCCCC
DDDDDDDDDDDDD
}


I would like locate each block above by the word "Word1" and then insert a string of text (Ts) in the 4th line of each block as such:

Word1 AAAAAAAAAAAAAA {
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC
DDDDDDDDDDDDDD
TTTTTTTTTTTTTT
}


Word1 BBBBBBBBBBBBBB {
ZZZZZZZZZZZZZZ
WWWWWWWWWWWWWW
QQQQQQQQQQQQQQ
TTTTTTTTTTTTTT
}


Thanks for your help.


0

Response Number 3
Name: nails
Date: September 20, 2005 at 09:30:32 Pacific
Reply:

I'm trying to identify a pattern. Does 'Word1' always start at the beginning of the line in line 1 of the block?

Can we search just for 'Word1' at the beginning of the line and not worry about whether it's the first line of the block?


0

Response Number 4
Name: lamos
Date: September 20, 2005 at 10:52:57 Pacific
Reply:

Yes, the word "word1" starts at the beginning of each block (as a reference point). From the reference point one, a string is inserted on the 4th line within the "{ }". The reason for this is that there are other blocks that start with a "word" and a number, like word12, word45, wordxy, etc. I want to target a specific word, in this case "word1".

Thanks.


0

Response Number 5
Name: nails
Date: September 20, 2005 at 11:31:27 Pacific
Reply:

Here's an awk script that prints the line of TTTs after the 4th line if the beginning of the line matches "Word1":

#!/bin/ksh

awk ' BEGIN { allt="TTTTTTTTTTTTTTTTT"; cnt=0; prn=0 }
{
if($0 ~ /^Word1/)
prn=1

if(prn == 1)
cnt++

printf("%s\n", $0)
if(prn == 1 && cnt == 4)
{
prn=0
cnt=0
printf("%s\n", allt)
}

} ' datafile.txt > newdatafile.txt



0

Related Posts

See More



Response Number 6
Name: lamos
Date: September 20, 2005 at 12:06:42 Pacific
Reply:

Nils,

It's perfect!

Thanks a bunch....


0

Sponsored Link
Ads by Google
Reply to Message Icon

Replacing a word in a fil... how to enable sound card ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to insert string in a list?

Insert string in a txt file..? www.computing.net/answers/unix/insert-string-in-a-txt-file/6089.html

How to delete lines in a file based on string www.computing.net/answers/unix/how-to-delete-lines-in-a-file-based-on-string/8521.html

How to run script remotely in a silent m www.computing.net/answers/unix/how-to-run-script-remotely-in-a-silent-m/2168.html