Replace Entire line if any part matches regexp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Entire line if any part matches regexp
# 1  
Old 09-14-2008
Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and have a column of address that matches up with other customer info. Some of the customers' records do not contain address info, but all of them contain the field " CONTACT: Name" after the adress, like so:

Address line 1
Address line 2
CONTACT: Name

Address line 1
Address line 2
CONTACT: Name

Can anyone help me turn the CONTACT line into a delimeter, or suggest something simpler, I would GREATLY appreciate it as there are many entires. NOTE: Not all address contain 2 lines, some have 4, some have 0. ALL of them contain the CONTACT line
# 2  
Old 09-14-2008
Try this:

Code:
awk '/CONTACT:/{print $2";"s; s="";next}{s=s?s";"$0:$0}' file > newfile

Regards
# 3  
Old 09-14-2008
Hey Franklin, no dice on that, but I ended up figuring out a much simpler way to do it. I used:
Code:
sed 's/CONTACT.*/;/' file

to replace those lines with colons. I then deleted all newline characters with 'tr'. This left me with one line of addresses delimited by semicolons. I exported it into excel which gave me one row of 1282 addresses. Each address filled one cell on one line. I coped the whole row, and 'Paste Special'ed it into my master document using transpose to make it a column in excel as it could not handle the newlines in the delimited fields. I can export this file to CSV,TXT,XML etc., but is there an easy way to format the addresses so they aren't one line?
# 4  
Old 09-14-2008
Post a portion of your input file and the desired output.

Regards
# 5  
Old 09-14-2008
I have addresses like this

34 West Gutierrez Street Santa Barbara CA 93101; PO Box 6666 Santa Barbara CA 93120 USA United States;...

All one one line of a text file that I exported into Excel. The thing is, I need to import this into Quickbooks, and I want properly formatted addresses. Newlines in the text were problematic when importing, so I got rid of them. I thought I might be able to go back to when before I deleted newlines, and changed them into some random character, then somehow replace that character with a newline in excel, like so.

;
PO Box 6666
Santa Barbara CA 93120
USA United States
;

Not all the addresses have the same amount of lines, and some have no country line. the only other pattern I see is the that there should be a newline instead of a space which is right before the blank space in between "City" "State", and then another newline after the zip.

It's kind of complicated, but basically I just want to have a formatted address in ONE cell in excel, rather then one entire line of the address in one cell.

Hope this makes it clear, thanks in advance!
# 6  
Old 09-14-2008
IMHO it's not possible to have several lines in one cell in excel or have I missed something?
# 7  
Old 09-14-2008
You could be right, I was not able to hit enter in a cell to create a newline character. I am not yet sure if excel will take the address broken up by City State Zip etc. Thanks for your help though Franklin. I'll follow up on this when I find a solution... err maybe another feasible problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace matches string in each line with the arrayvalue in shell

I have a list of value , and need to replace that in my file. Eg: File1 tcname:fail tcname: Pass tcname:skipped File2: 01,02,03 Output: File 1 01:fail 02: Pass 03:Skipped (8 Replies)
Discussion started by: DevAakash
8 Replies

2. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

3. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

4. Shell Programming and Scripting

Help with sed to replace entire line

Hi, I need to replace an entire mailx line as follows using sed: sed -e 's/<line1>/<newline>/g' <filename> But I am getting comman garbled error since the new line has many special characters. I enclosed allspecial chars in \ but still no use. Can any one help me? Please use code... (2 Replies)
Discussion started by: vinodhin4
2 Replies

5. Shell Programming and Scripting

Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

Hey Fellas. I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv... (6 Replies)
Discussion started by: midniteslice
6 Replies

6. Shell Programming and Scripting

Replace entire line

I want to replace one line from my configuration file with the new settings. The file Name: /etc/httpd/conf/httpd.conf The following line should be replaced with the line mentioned below. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "\"%h\"... (3 Replies)
Discussion started by: shantanuo
3 Replies

7. UNIX for Advanced & Expert Users

command to replace the entire line from the key point

Hi everyone I am new to Unix. I got stuck up by small issue. I have text file something like this abc 'xyz' '5' lmn 'pqr' '7' i want to replace the abc 'xyz' '5' to abc 'xyz' '6' but i have a key as 'xyz' based on this key i want to do that. I am not aware of how to use sed... (7 Replies)
Discussion started by: Vijayaragavan
7 Replies

8. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

9. Shell Programming and Scripting

sort entire line based on part of the string

hey gurus, my-build1-abc my-build10-abc my-build2-abc my-build22-abc my-build3-abc basically i want to numerically sort the entire lines based on the build number. I dont zero pad the numbers because thats "how it is" ;-) sort -n won't work because it starts from the beginning. ... (10 Replies)
Discussion started by: gurpal2000
10 Replies

10. Shell Programming and Scripting

Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file sub changestate { my $base = (); my @base = (); open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~... (1 Reply)
Discussion started by: insania
1 Replies
Login or Register to Ask a Question

Featured Tech Videos