Solved

How to use AWK/SED to insert a line?

Posted on 2008-10-01
5
1,552 Views
Last Modified: 2013-12-26
I have a file that looks like this:

Mary=Ann
Ann=Mary
Fake=Fake

Joe=John
Doe=Doe



I want to find Fake=Fake and insert another line before it. This change then needs to be written back to the same file. The replacement value is in a variable, say, $__changes

I have been trying sed but for some reason it creates a 0byte file if the source and target files are the same. I am ok if you can provide an awk solution.

__changes='$1.something'

cat 'file1.txt' | sed '
/Fake=FakeX/ i\
$__changes
' > 'file1.txt'

This doesn't work if I try a variable in the replacement text.

Please note I need to insert before only the first instance of the match.

Thanks for your help.
0
Comment
Question by:infa007
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22613806
>'file1.txt
opens and clears the output before sed runs
try sed -i
0
 
LVL 84

Expert Comment

by:ozo
ID: 22613869
sed -i.bak -e '/Fake=Fake/ i\
'$__changes'
'  file1.txt
0
 

Author Comment

by:infa007
ID: 22615073
I get an error: sed: Not a recognized flag: i
0
 
LVL 1

Accepted Solution

by:
gisellla_igr earned 500 total points
ID: 22615298
try this

file=file1.txt

tmpFile=${file}$$

__changes="nueva linea"

cat $file | sed "

/Fake=Fake/ i\\

$__changes

" > $tmpFile

mv $tmpFile $file

Open in new window

0
 

Author Closing Comment

by:infa007
ID: 31501945
Thanks, exactly what I wanted!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now