Hold, Replace and Print with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hold, Replace and Print with sed
# 1  
Old 06-06-2012
Hold, Replace and Print with sed

Hi,
I'm a newbie with scripting so I'd appreciate any help.

I have a file import.txt with below text
AA_IDNo=IDNoHere
AA_Name=NameHere
AA_Address=AddressHere
AA_Telephone=TelephoneHere
AA_Sex=SexHere
AA_Birthday=BirthdayHere
What I need is that the Lines for Name, Address and Telephone would be duplicated and "_New_" would be inserted after AA. Below is a sample output.
AA_IDNo=IDNoHere
AA_Name=NameHere
AA_New_Name=NameHere
AA_Address=AddressHere
AA_New_Address=AddressHere
AA_Telephone=TelephoneHere
AA_New_Telephone=TelephoneHere
AA_Sex=SexHere
AA_Birthday=BirthdayHere
I'm thinking using sed for this? Please if someone can shed some light.
# 2  
Old 06-06-2012
Command:
Code:
sed 's/\(AA_\)\(Name.*\)/\1\2\n\1NEW_\2/;s/\(AA_\)\(Address.*\)/\1\2\n\1NEW_\2/;s/\(AA_\)\(Telephone.*\)/\1\2\n\1NEW_\2/' import.txt

Results:
Code:
AA_IDNo=IDNoHere
AA_Name=NameHere
AA_NEW_Name=NameHere
AA_Address=AddressHere
AA_NEW_Address=AddressHere
AA_Telephone=TelephoneHere
AA_NEW_Telephone=TelephoneHere
AA_Sex=SexHere
AA_Birthday=BirthdayHere

---------- Post updated at 05:00 PM ---------- Previous update was at 04:55 PM ----------

That can be simplified to:
Code:
sed 's/\(AA_\)\(Name.*\|Address.*\|Telephone.*\)/\1\2\n\1NEW_\2/' file3

This User Gave Thanks to ddreggors For This Post:
# 3  
Old 06-06-2012
This sed command worked for me(GNU sed version 4.2.1):
Code:
sed -n 'p;s/AA_\(Name\|Address\|Telephone\)\(=.*\)/AA_New_\1\2/p' aa_test.txt >aa_test_2.txt

## File(aa_test.txt) contains:
AA_IDNo=IDNoHere
AA_Name=NameHere
AA_Address=AddressHere
AA_Telephone=TelephoneHere
AA_Sex=SexHere
AA_Birthday=BirthdayHere

## File(aa_test_2.txt) now contains:
AA_IDNo=IDNoHere
AA_Name=NameHere
AA_New_Name=NameHere
AA_Address=AddressHere
AA_New_Address=AddressHere
AA_Telephone=TelephoneHere
AA_New_Telephone=TelephoneHere
AA_Sex=SexHere
AA_Birthday=BirthdayHere

# 4  
Old 06-07-2012
Just a quick point, after look at both of our (spacebar's mine) sed commands... it is even simpler to write as:

Code:
sed -n 'p;s/AA_\(Name\|Address\|Telephone\)/AA_New_\1/p' file3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed's hold-space to filter file contents

I wrote an awk script to filter "uninteresting" commands from my ~/.bash_history (I know about HISTIGNORE, but I don't want to exclude these commands from my current session's history, I just want to avoid persisting them across sessions). The history file can contain multi-line entries with... (6 Replies)
Discussion started by: ivanbrennan
6 Replies

2. Shell Programming and Scripting

Need help for sed replace and print

Hi I am working with sed to get string replace and print all the lines. Cat f1 <text1> tag123 44412c232place1 text456-text= tag12 44412c232place4 jjaa TAG456 44412c232place1066dfdf erer .. i have used this command - sed -n '/tag/ s#place#SomePlace#gp' f1 It gives me... (2 Replies)
Discussion started by: krsnadasa
2 Replies

3. Shell Programming and Scripting

Hold buffer in sed

Hi Experts, i have a file like below **** table name is xyz row count for previous day 10 row count for today 20 diff between previous and today 10 scan result PASSED **** table name is abc row count for previous day 90 row count for today 35 diff between previous and today 55... (4 Replies)
Discussion started by: Lakshman_Gupta
4 Replies

4. Shell Programming and Scripting

Scripting a global find and replace in an VME output print file

Hi Folks, Below is an extract from a VME Print file which gets handed over to a print house. The problem I have is not that tricky rther looking for a way to handle it in a simple and clean way. Is to first select all lines with "0058" which have four spaces so "0058 " as the selcetion... (3 Replies)
Discussion started by: Gary Hay
3 Replies

5. Shell Programming and Scripting

read is not on hold

In end of https://www.unix.com/shell-programming-scripting/103227-while-read-loop-scope-variables-shell.html mjd_tech gives script which can read some values directly without manually input, but if no value is the right one, my understand is, it will on hold for waiting the next input, but when I... (7 Replies)
Discussion started by: newoz
7 Replies

6. Shell Programming and Scripting

sed: hold buffer question

I've been using sed to help with reformatting some html content into latex slides using the beamer class. Since I'm new to sed, I've been reading a lot about it but I'm stuck on this one problem. I have text that looks like this: ******************* line of text that needs to be... (4 Replies)
Discussion started by: tfrei
4 Replies

7. Shell Programming and Scripting

sed pattern and hold space issues

Good day. Trying to make a sed script to take text file in a certain format and turn it into mostly formatted html. I'm 95% there but this last bit is hurting my head finally. Here's a portion of the text- Budgeting and Debt: Consumer Credit Counseling of Western PA CareerLink 112... (5 Replies)
Discussion started by: fiendracer
5 Replies

8. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

9. Shell Programming and Scripting

injecting new line in sed substitution (hold space)

Morning, people! I'd like to call upon your expertise again, this time for a sed endeavor. I've already searched around the forums, didn't find anything that helped yet. background: Solaris 9.x, it's a closed system and there are restrictions to what is portable to it. So let's assume I... (4 Replies)
Discussion started by: ProGrammar
4 Replies

10. UNIX for Dummies Questions & Answers

How to Find /replace and print?

Hi, I have one txt file, and I want replace 2 diffrent texts with somther text and the same time, I want to send that to print.. something like sed -e 's/Times-Roman/Helvetica/'|sed -e 's/Times/Helvetica/' oldfile > newfile < lp is this will workout? any idea? (1 Reply)
Discussion started by: redlotus72
1 Replies
Login or Register to Ask a Question