using sed to replace a specific string on a specific line number using variables


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed to replace a specific string on a specific line number using variables
# 1  
using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables

this is where i am at

grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean
grep -n Destination output.clean > output.1
awk -F : '{print $1}' < output.1 > output.2
for y in `cat output.2`; do
for port in `cat portlist` ; do
echo $y
sed -e "'$y's/Destination Port matches/Dest port match $port/" < output.clean >> output.3

done
done


where the first grep cleans out the file of junk, the second one gets the line numbers i need to look at, awk cleans that file quick, and sed is supposed to use the values to find the line number and grep place it with the text of which $port being var that gets assigned.

Many thanks!

ps: i am a complete rookie
# 2  
Quote:
Originally Posted by todd.cutting
using sed to replace a specific string on a specific line number using variables
...
...
for y in `cat output.2`; do
for port in `cat portlist` ; do
echo $y
sed -e "'$y's/Destination Port matches/Dest port match $port/" < output.clean >> output.3

done
done

...
Your code does not seem to match the logic.
The outer for loops through line numbers in output.2 (say, 1,2).
The inner for loops through port numbers (?) in portlist (say, 10,20) for each line number.
So, you essentially have something called a cartesian product here. For the data shown above, you have:

Code:
Iteration 1 => Outer: 1 - Inner: 10
Iteration 2 => Outer: 1 - Inner: 20

Iteration 3 => Outer: 2 - Inner: 10
Iteration 4 =>  Outer: 2 - Inner: 20

Now, assuming that you have found out a way to determine the line number, the two for loops would work as follows:

(Iteration 1) Put port no. 10 in line 1.
(Iteration 2) Put port no. 20 in line 1, thereby overwriting the port no. put in iteration (1).
(Iteration 3) Put port no. 10 in line 2.
(Iteration 4) Put port no. 20 in line 2, thereby overwriting the port no. put in iteration (3).

As you can see, this does not meet your stated goal of "replacing a specific string on a specific line number".

For this, you need a one-to-one relation between a line number and that specific string (which I assume is the port number).

Given below is a very simple file whose data shows such a relation:

Code:
$ 
$ cat output.2
2 222
5 555
6 666
7 777
$

So now, I want to put
- the string "222" in line no. 2,
- the string "555" in line 5,
- the string "666" in line 6 and
- the string "777" in line 7
of the file, say, "output.clean".

Code:
$ 
$ cat output.clean
this is line 1
Destination port matches
this is line 3
this is line 4
Destination port matches
Destination port matches
Destination port matches
this is line 8
$ 
$

In fact, in such a case, you do not even need to search for "Destination port matches". That's because the substitution is based on the line number and not on a string match.

An awk program can be written for the same:

Code:
$ 
$ awk '{if (FILENAME=="output.2"){a[$1]=$2}else{if (a[FNR]==""){print $0}else{print "Dest port match",a[FNR]}}}' output.2 output.clean
this is line 1
Dest port match 222
this is line 3
this is line 4
Dest port match 555
Dest port match 666
Dest port match 777
this is line 8
$ 
$

You may want to brush up some concepts here.
- FILENAME stores the name of the file being currently processed.
- FNR is the record number of the current file.
- The first if condition checks if the FILENAME is "output.2" and if so, it creates an associative array with the line number as the key and the port number as its value.
- The else part goes to the "output.clean" file.
- If the current line number of "output.clean" is not a key of the associative array (created earlier), then simply print the line.
- Otherwise, print the string you want with the hash value in it.
- Again, since the associative array must be created before "output.clean" is processed, the order of files has to be correct.

HTH,
tyler_durden
# 3  
with the below i seem to get getting better results since the orig post
Code:
grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' > output.clean
for x in `cat portlist` ; do
        sed 's/Destination Port matches/Desp port Match $x/' < output.clean >> output.final
done

my output is
Code:
Desp port Match $x
========================


but i cant seem to get the x var to print its value.... BTW i just couldnt follow your suggestion its been way too long since i did any of this and i wasnt event that good when i was doing it more often.

any thoughts on how to get the value of the var to print to the end of the line?


many thanks!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #854
Difficulty: Medium
React (also known as React.js or ReactJS) is a JavaScript library maintained by Facebook for building user interfaces.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

4. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

7. UNIX for Dummies Questions & Answers

How to replace specific number in a file

Hi, i'm trying to find a way to replace some numbers in a file, but with no luck sofar. Let's say i have file with three columns, i would like to find specific line, and replace third column with different number. search_string="oa11 /home/testfile1.log" new_number=600 test file... (4 Replies)
Discussion started by: dusoo
4 Replies

8. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

Featured Tech Videos