Replacing entire fields with specific text at end or beginning of field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing entire fields with specific text at end or beginning of field
# 1  
Old 07-16-2015
Replacing entire fields with specific text at end or beginning of field

Greetings.

I've got a csv file with data along these lines:

Code:
Spumoni's Pizza Place, Placemats n Things, Just Lamps
Counterfeit Dollars by Vinnie, Just Shades, Dollar Store

I want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar", but not if those words are simply in the middle of the field. So the desired result would be something like:

Code:
SPECIAL, Placemats n Things, Just Lamps
Counterfeit Dollars by Vinnie, Just Shades, SPECIAL

How would I do this using awk, sed, csh, or whatever is the right tool for the task? Thanks!

Last edited by jim mcnamara; 07-16-2015 at 11:16 PM..
# 2  
Old 07-16-2015
Hi, if there are no TABS, try:
Code:
awk '{for(i=1; i<=NF; i++) if($i~/^ *Dollar|Place *$/) $i="SPECIAL"}1' FS=, OFS=, file


--
otherwise use $i~/^[ \t]*Dollar|Place[ \t]*$/
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-17-2015
Awesome, thanks, I get the premise!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting text on multiple lines, then removing a beginning and end patterns

I have a file similar to the below. I am selecting only the paragraphs with @inlineifset. I am using the following command sed '/@inlineifset/,/^ *$/!d; s/@inlineifset{mrg, @btpar{@//' $flnm >> $ofln This produces @section Correlations between seismograms,,,,}} ... (5 Replies)
Discussion started by: Danette
5 Replies

2. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

sed to add field heards to specific fields

I have tab delimited input that prints out in the format below: I am trying to add field headers to $5 and $6. Not sure if sed is the best tool but my attempt is below. Thank you :). $5 = REF $6 = ALT file ID CHR START STOP 123 1 100 200 A ... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

5. Shell Programming and Scripting

replacing field in specific line in a file

Hi, I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's... (5 Replies)
Discussion started by: vytenis
5 Replies

6. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

7. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies

8. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

9. Shell Programming and Scripting

replacing a nul field with text

Ok here's my pickle. I have a file in which every line must be the same length. Each field within the line is a certain length. None of these can be changed. What I need to do is look at a specific field within this file, let's say it starts with character 30 and ends with 50. If this field is... (3 Replies)
Discussion started by: DarkHound
3 Replies

10. HP-UX

replacing text in specific location

i have a file that looks like this: 000000112/01/2008 D99999 000000 12/01/2008 D99999 000000 12/01/2008 1D99999 i need to replace the blanks into 1 for column 7,18-19 how can this be achieved using awk? Thanks. (1 Reply)
Discussion started by: zeontman
1 Replies
Login or Register to Ask a Question