Programming :: Insert Line Of Text Prior To Pattern Only Once?

Jun 29, 2010

I have a file like so:

Code:

one
two
three
four

I would like to insert prior to the word "three" all items from this second file with the following contents:

Code:

four
three
two
one

Now my issue is, and I have been using both sed and awk currently, that after the second line of the new file is read there will of course now be 2 copies of the word "three" but I would like to only insert the final 2 words, ie "two" and "one" prior to only the first occurrence of the word "three" so final file will look like:

Code:

one
two
four

[code]....

So here there is now only one of each word from the second file joined to make the new file. For simple code I have tried something like the following:

Code:

while read line
do
awk -v n=$line '!f && /three/{print n;f++}1' file1 > tmp_file
mv tmp_file file1
done < file2

Now this works but seems very clumsy to me. There is obviously a better sed and / or awk out there.

View 5 Replies


ADVERTISEMENT

Programming :: Search A File For A Particular Pattern And If Pattern Found Replace The Line With New Text?

Feb 24, 2010

I want to search a file for a particular pattern and if pattern found replace the line with new text. i am using awk 'match($0,"pattern") != 0 {print $0} ' filename to check if the pattern exists.how do i get the line number of the pattern and delete that line and replace the line with my new text?

View 1 Replies View Related

Programming :: Insert Text Before The First Pattern Found?

Apr 8, 2011

i need to add this line nameserver 208.67.122.221 from ISP before the first nameserver already exists in resolv.conf in all workstations i know

Code:

sed '/nameserver/ i
ameserver 208.67.122.221'

but that insert it on every line after nameserver not only before the first one

Code:

# blah blah
# blah blah
domain mydom.com

[code]....

View 3 Replies View Related

Programming :: Shell Script To Delete Part Text Of A Line If Pattern Matches?

Apr 12, 2010

I am trying to create a shell script, on taking a input file as parameter, which need to do 3 things

1) create a copy of existing file.

2) add a new line to the copied file.

3) strip off all the absolute paths inside the copied file

The first 2 points are straight forward. but i am finding it difficult to acheive the 3rd point. myself not very good with awk and sed. but gave it a shot in vain. For example, the input script consists of below,

PROGRAM=`/usr/bin/basename $0`
HOST=`/usr/bin/uname -n`
echo Start $PROGRAM `/usr/bin/date` |
/usr/bin/tee -a $LOG

The output of the script should look like,

PROGRAM=`basename $0`
HOST=`uname -n`
echo Start $PROGRAM `date` |
tee -a $LOG

View 14 Replies View Related

Programming :: Sed To Read The File And Replace And Insert The Pattern?

Jun 11, 2009

I want to read from the file and check for the pattern, if the line has some word like <string>: then string should be copied into buffer. Afterwards, I want to insert the same <string> with some word in the next line of the file. use sed command to perform the above mentioned operations?

View 2 Replies View Related

General :: Insert Text To A Specified Line?

Aug 23, 2009

Code:

sed -n '123p' file

that will print line 123 but how might I insert text to a specified line?

View 3 Replies View Related

Programming :: Insert Line Using Sed Or Awk At Line Using Line Number As Variable

Jul 25, 2011

I want to insert a line at a particular line number using sed or awk. where line number is not fixed and is in form of variable.

I want to use variable in sed or awk command.I tried something like below, but no luck.

View 7 Replies View Related

General :: Insert Text In The Last Line Of A File With Sed Command?

Oct 4, 2010

I want to insert the text "DB dept" in the last line of passwd file which looks like this right now

Quote:

newuser:x:717:717::/home/sacharje:/bin/bash

And I want it to be like this:

Quote:

newuser:x:717:717: DB dept:/home/sacharje:/bin/bash

I tried to do that with sed but failed, I'd like to know the proper syntex with sed to achieve this.

View 7 Replies View Related

General :: Insert Text To A File Using Command Line?

Jan 28, 2010

I need to be able to edit a file from the commend line as I intend to script this operation, the file is called menu.lst The original is as below

Code:

titleUbuntu 8.04.3 LTS, kernel 2.6.24-24-generic
root(hd0,0)
kernel/boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro quiet splash

[code]....

View 4 Replies View Related

Programming :: Insert A Line/value After A Particular Line In File With Sed?

Jan 13, 2010

I need to run a command in a shell script to insert a line in a file, after it finds certain line. To add the line 'user = mysql' after the line [mysqld] in file /etc/my.cnf

View 3 Replies View Related

Programming :: Delete Line Containing A Pattern?

Apr 1, 2011

if the given pattern exists in the file with the very next line starting and endingwith the same pattern , delete the line that starts and ends with the given pattern.So upon running on this file

hai people<PATTERN> we had
<PATTERN>a lot of fun<PATTERN>
writing scripts

[code]....

View 6 Replies View Related

Programming :: Sed: Delete Last Line Matching A Pattern?

Mar 27, 2011

I have a question about sed programming, actually a one-liner for which I cannot find a solution, right now. I need to delete a line matching a specific pattern only if it is the last line. In practice, I would put together the following:

Code:
#
# This deletes the last line of a file

[code]...

View 5 Replies View Related

Programming :: S/ Command Won't Replace Two Occurrences Of Pattern On Same Line

Dec 15, 2009

Here's the actual line of code, which exists in a bash script:

Code:

I want to replace instances like this:

Code:

with this:

Code:

Using this:

Code:

Which works great when there's only ONE of the pattern on the line. But in a case like the "actual line" I posted first, where there are two patterns, separated by a slash, only ONE gets replaced

Watch:

Code:

Why? There must be (among many other things) something I'm not knowing about sed, that's causing this.

-- I'm currently using the ~ (tilde) as the separator in the sed command. It doesn't matter, I've used / ~ and % with no difference.

-- As a test, I tried putting a different character(s) in the middle of the original pattern instead of the / but that made no difference.

-- I've come up with various similar but slightly different regexs that will do this replacement, but they all have had this same result.

-- I tried the sed single-quoted, double-quoted, and unquoted; the latter fails to execute, and the formers both work as described here: wrong.

View 7 Replies View Related

Programming :: Insert Text Message In Textarea Using Php?

Dec 13, 2010

i trying to add text messge in textarea and i got error in connection and i don't know if it correct syntax. and i wonder when i was install xamp server it no password required. but i dont know what password that i input in my php connection.?

PHP Code:

<?php
$con = mysql_connection("localhost","root","123");
if(!$con)
{

[code]....

PHP Code:

<textarea rows="3" cols="30" name="txtcomment" wrap="physical">Leave a Comment

View 4 Replies View Related

Programming :: Insert A Character In Line Using Sed/awk?

Jul 25, 2011

I have a string as below

LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined

Want the above string to be modified as

LogFormat "%h %l %u %D %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined

inserting %D after %u in the string

View 3 Replies View Related

Programming :: Insert Paragraph At Particular Line?

Jul 26, 2011

I want to copy below content to a file after line 200.

Alias /A/ "/A/B/C/D/"
<Directory "/E/F/G/H/">
Options Indexes MultiViews
AllowOverride None

[Code]....

Or is it possible if I can copy this content in some other file and insert to the main file.

View 4 Replies View Related

Programming :: Multiple Line Pattern Matching And Variable Extraction?

Oct 19, 2010

I have this complex log file filled with entries like

test1-G1/0/0-100-QOS-7001923-ROUTING (ClassMap)
Action: Resolved New
sysName: test1.local

[code]...

View 1 Replies View Related

Programming :: Insert A Line In The Test File With Sed?

Jul 15, 2010

I have following file workers file: There are 800 entries like this.

# consensus/uat
worker.consensus-uat1.host=lonlx10409
worker.consensus-uat1.port=13702
worker.consensus-uat1.type=ajp13

[code]....

I want to insert one line just below the line starting with "woker.list". I think sed can do it but I am not aware.

View 14 Replies View Related

Programming :: Sed Insert Line With Leading Spaces?

Mar 3, 2011

I'm trying to insert a line using sed that has leading spaces before the text. Sed seems to be just dropping the spaces and only inserting the text. Any ideas what I'm missing?

Code:

NAM=rb134
sed -i.bak -e "$i
host ${NAM} {" /etc/crap

Instead of inserting a line with 8 leading spaces inserts it with "host" at the beginning of the line. I tried

Code:

NAM=rb134
sed -i.bak -e "$i
^ host ${NAM} {" /etc/crap

but it put the "^" at the begging of the line.

View 6 Replies View Related

Programming :: Sed One-liner: Search Text In File And Replace It - If Not Present Insert It?

Oct 16, 2010

As indicated in the subject, I want to search a text. If the text is present I want to replace it. But if the text is not present, I want to insert it after first line and before last line.

Searched text is:CleanCache "*";

Where * can be anything.

Example: CleanCache "false"; -> CleanCache "true";

If CleanCache "false"; is not present, only insert CleanCache "true"; after first line and before last line.

View 8 Replies View Related

General :: Insert A Line Into A Text File Using "sed" Command?

Sep 18, 2010

I've been reading tutorials of Linux sed command, but haven't got anything yet. the problem is : I want to insert a line into my DNS database file which has a pattern like below:

<Domain name> 3tabs here <IN> <A> <ip address>

the question is : how to add a line into a file like this using linux sed command? I have problem inserting tabs and the spaces!

View 7 Replies View Related

Programming :: Delete Line Of Text From Text File Via Shell?

Jan 13, 2010

I have to delete a certain line of text from the a textfile via ubuntu's shell scripting.I have done research, and it seems that most people advocate the usage of sed /d option. sed makes does not edit the text file. Hence, most options I discovered involved the use of a temporary variable/textfile and then overwriting the old file with the temporary new file. Is there anyway whereby I can bypass the use of temporary storage containers? I hope there is any magical combination of commands to edit the file directly.

View 3 Replies View Related

Programming :: BASH: Each Line Of Multiple Text Files Gets Added To One Line?

Sep 12, 2010

I currently have 3 files with floating point data that I wish to have in a single file with the format:

Code:

F1 F1 F3 Output
a1 b1 c1 a1 b1 c1
a2 b2 c2 a2 b2 c2
a3 b3 c3 a3 b3 c3
a4 b4 c4 a4 b4 c4

View 3 Replies View Related

Programming :: Open Two Text Files - Read Them Line By Line And Update Parameters Of The 3rd File ?

Oct 18, 2010

I have two txt files containing x and y coordinates: xcoord.txt & ycoord.txt. I need to open them; read them line by line to get each coordinate; then each time I need to update Xs and Ys parameters inside another file called "dc.in" with the grabbed values.

Finally each time I need to run two exe files ( dc_2002 and st_vac) and produce corresponding output for each Xs and Ys ( dc.in is an input file for this exe files)

I have written the following code but it does not work:

View 14 Replies View Related

Programming :: Perl - Delete Line From Text File With Duplicate Match At Beginning Of Line

Apr 1, 2009

Was wondering if any perl guru's could help me with a quick log file adjustment. I have a text file that looks like so (tabs and newlines are revealed so you can see what separates the data):

There are maybe 100 lines of text in this file at any given time. I need to delete all duplicate lines only looking at the first bit of text prior to the first tab. It doesn't matter which one gets deleted as long as there are no two lines that begin with that same text at the beginning before the first tab. So in this example, either the fist line "1234" or the last line "1234" would need to be deleted. I already have code in my script that opens the files - I just need the code to read the text into an array and the part that would find matches based on the above criteria, and make the deletions.

If it would be easier, I can even do a system call and use SED (v4.1.5) and/or AWK (3.1.5) instead.

View 7 Replies View Related

Programming :: Add Text Before Line Number In Text File?

May 3, 2010

a sed command to add a text before line number in text file? I have text file with 500 lines, and i want to add 3 more lines with text after line 300, OR before line 302, isn't no problem.

View 16 Replies View Related

Programming :: Bash: Read A Text File Line By Line?

Jul 7, 2011

bash 3.1.17(2) I'm trying do write a shell script which must operate on each line of an ASCII text file. So, all the code must be inside a loop, and inside the loop, the first thing should be to read the next line from the file. I have the bash read command. But it reads from stdin. Any way to make read from a file?

View 6 Replies View Related

Programming :: Using Python To Merge Two Text Files Line For Line

Jul 6, 2010

I'm a bit new to Python programming and hoped that someone might be able to help with a problem I'm having. What I essentially want to do is to combine two text files line for line. I know how to do this in a bash script so to give you a better idea here's the code for that:

Code:

This is basically for adding on values to the end of a CSV file that uses ';' as the delimiter. So say file1 said:

And file2 said:

Then running this command would create merged_file1_and_file2 which would be:

The code I'm using at the moment is:

Code:

As I'm sure any experienced python programmer will see, this prints out the first line of the file "csvraw" and then all of the lines of "stamps" and then the remainder of "csvraw".

What I'd like to do is something like: (pseudo code, I know it's not python ;-))

Code:

Is this possible? I've tried googling and my Python Pocket Reference hasn't been much help. I've looked at pickling but that doesn't seem appropriate.

View 1 Replies View Related

Programming :: If Statement Pattern Search / End Of Pattern Special Character?

Apr 29, 2010

I have to enhance the behaviour of a backup script written in perl. I don't need to change it, what I need to do is to create a bash script that does some checks like file name and file size, execute the backup script then check if the backup files match the original files.Here's how I try to do it:

- read the files from the original files folder
- store them in an array
- search in the array the files that have a specific file extension
- store the file names that match the search pattern (I know the backup script skips some files so I can hardcode the search pattern)
- run the backup script
- read the files from the backup folder
- store them in an array
- compare the original files name and size stored in an array with those from the backup folder
- send a report email

View 3 Replies View Related

Programming :: Text On A Specific Line At The End Of A Line?

Jul 26, 2010

I'm trying to add text to a file for a specific group of users, I'll need to do examples as I can't think of an easy way of explaining, my file is like this:

Code:

users{
user1
user2

[code]....

At the present my code lists all the available groups, how would I add a user to a specified group? (e.g add "members user3") to the end of group 1 for example. So the code ends up like this

Code:

members user2,user3

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved