sed or awk Replacement/Addition

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed or awk Replacement/Addition
# 1  
Old 04-10-2018
sed or awk Replacement/Addition

Hi,

I have a big text file with similar data as below and need the text as in output
using awk or sed. any help is greatly appreciated.

Input:
Code:
City=Chicago Elden street 
 >>> reservedBy = business 1
 >>> reservedBy = business 2
 >>> reservedBy = business 3
City=Dallas Elm street 
 >>> reservedBy = business 1
 >>> reservedBy = business 2
 >>> reservedBy = business 3

Output:
Code:
City=Chicago Elden street
Chicago  >>> reservedBy = business 1
Chicago  >>> reservedBy = business 2
Chicago  >>> reservedBy = business 3
City=Dallas Elden street
Dallas  >>> reservedBy = business 1
Dallas  >>> reservedBy = business 2
Dallas  >>> reservedBy = business 3

Moderator's Comments:
Mod Comment Please use CODE (not HTML) tags as required by forum rules!

Last edited by RudiC; 04-11-2018 at 04:21 AM.. Reason: Changed HTML to CODE tags.
# 2  
Old 04-11-2018
Hello tech_frk,

Could you please try following and let me know if this helps you.
Code:
awk -F"[ =]" '/^City/{val=$2;print;next} {print val "\t" $0}'  Input_file

Thanks,
R. Singh
# 3  
Old 04-11-2018
What if the city has more than one word like "New York" or "Los Angeles"?
# 4  
Old 04-11-2018
Quote:
Originally Posted by RudiC
What if the city has more than one word like "New York" or "Los Angeles"?
Hello RudiC,

Good point Smilie
Then for these kind of scenarios following may help on same then.
Code:
awk -F"[ =]" '/^City/{val="";for(i=2;i<=NF-3;i++){val=val?val OFS $i:$i};print;next} {print val "\t" $0;}'  Input_file

Thanks,
R. Singh
# 5  
Old 04-11-2018
OK, solved. What if a street has three or more elements as "East New York Ave"?

The sample in post#1 has trailing spaces that don't help either.
# 6  
Old 04-11-2018
Quote:
Originally Posted by RudiC
What if a street has three or more elements as "East New York Ave"?
Or, more general, what separates the Cities name from the street name?

Because this is one problem where the "Hold space" capability of sed can be shown here is a solution (for one-word-cities) in sed:

Code:
sed '/^City=/ {
             h
             s/^City=//
             s/ .*/ /
             x
             p
             d
          }
     G; s/\(.*\)\n\(.*\)/\2 \1/' inputfile

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

4. Shell Programming and Scripting

AWK conditional addition

I have a column of numbers $2, I would like to add 360 to all numbers that are negative. This method seems a bit convoluted, and does not work (outputs 0): BEGIN { A=sprintf("%d", $2); if(A<0) A=A+360; BIN++; } END { for(A in BIN) print... (5 Replies)
Discussion started by: chrisjorg
5 Replies

5. Shell Programming and Scripting

Awk addition

How would I print out the total amount through awk? I tried using print "Total Amount: " $4+$4; Would I have to do a for loop to get through everything? (2 Replies)
Discussion started by: Boltftw
2 Replies

6. Shell Programming and Scripting

SED replacement

Hi, i have a file with lines, file.txt ------- test is fun testing is better I need to replace 'test' to 'develop' and i used, a=test b=develop sed "s,$a,$b,g" -------- but i see the word 'testing' is also replaced. Need some solution. Is there any way i could replace only 'test' ? (4 Replies)
Discussion started by: giri_luck
4 Replies

7. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

8. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

9. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question