Visit Our UNIX and Linux User Community


Grep or sed to search, replace/insert chars!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep or sed to search, replace/insert chars!
# 1  
Old 04-12-2014
Grep or sed to search, replace/insert chars!

HI All
Im trying to come up with an approach to finding a string, using a portion of that string to insert it on lines starting with the value "GOTO" appending to end of line after removing PT's ( See example below! )
EXAMPLE:
1. I would like to search for the line that starts with "TLAXIS/"
2. Extact from that string starting with the "," to eol (EX: ",-0.0841797,-0.9622495,0.2588236" )
3. Insert that value on every line in the same file starting with "GOTO" between last column and the "PT"
Note: PT & Numbers can be removed!
Code:
FROM:
SPINDL/1200
TLAXIS/,-0.0841797,-0.9622495,0.2588236
RAPID
FROM/.00000,.00000,1100.00000,.0000,.0000,1.0000
MODE/TRFORM,OFF
GOTO/.00000,.00000,1100.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO  /    -.00023,     .00017, 1060.00000                              PT     5
RAPID
GOTO  / -410.71830, -608.89040, 1060.00000                              PT     6
RAPID
GOTO  / -410.71780, -608.89070, 1017.22900                              PT     7
TO:
SPINDL/1200
TLAXIS/,-0.0841797,-0.9622495,0.2588236
RAPID
FROM/.00000,.00000,1100.00000,.0000,.0000,1.0000
MODE/TRFORM,OFF
GOTO/.00000,.00000,1100.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO  /    -.00023,     .00017, 1060.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO  / -410.71830, -608.89040, 1060.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO  / -410.71780, -608.89070, 1017.22900,-0.0841797,-0.9622495,0.2588236


Last edited by Franklin52; 04-12-2014 at 12:32 PM.. Reason: Please use code tags
# 2  
Old 04-12-2014
Show us desired output for that sample data.
# 3  
Old 04-12-2014
EXAMPLE

Summery, every value from ",-0.0841797,-0.9622495,0.2588236" is inserted at end on every line that starts with "GOTO"

Code:
SPINDL/1200
TLAXIS/,-0.0841797,-0.9622495,0.2588236
RAPID
FROM/.00000,.00000,1100.00000,.0000,.0000,1.0000
MODE/TRFORM,OFF
GOTO/.00000,.00000,1100.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO / -.00023, .00017, 1060.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO / -410.71830, -608.89040, 1060.00000,-0.0841797,-0.9622495,0.2588236
RAPID
GOTO / -410.71780, -608.89070, 1017.22900,-0.0841797,-0.9622495,0.2588236


Last edited by bartus11; 04-12-2014 at 06:03 PM.. Reason: Please use [code][/code] tags.
# 4  
Old 04-12-2014
Remember, you have to give filename twice at the end of command, instead of "file"
Code:
awk -F "[, ]" 'NR == FNR {if($1 == "TLAXIS/") {sub("TLAXIS/", X); v = $0}; next} $0 ~ /^GOTO/ && $(NF - 1) == "PT" {sub(" PT.*", v)}1' file file

# 5  
Old 04-12-2014
COOL! Trying it now! Question, can this be done with sed or grep? Just trying to understand various method! Thank in Advance! -mike
# 6  
Old 04-13-2014
The previous solution does not work for my example file. Also I don't see why one should process the file twice. Here is a simple one:
Code:
awk -F "[, /]+" '($1=="TLAXIS") {$1=""; v=$0; next} ($1=="GOTO") {sub(" *PT.*", ""); $0=$0 v} {print}' OFS=", " file

---------- Post updated at 11:01 AM ---------- Previous update was at 10:48 AM ----------

Because you need to store a value in the TLAXIS line, you cannot use grep.
With sed one can use its hold buffer; but compared to awk it would be harder, and the code less readable.

Last edited by MadeInGermany; 04-13-2014 at 12:54 PM..
# 7  
Old 04-13-2014
Quote:
Originally Posted by MadeInGermany
With sed one can use its hold buffer; but compared to awk it would be harder, and the code less readable.
I'd like to respectfully disagree. "Hard to write" and "readable" are relative terms. For someone used to using sed your awk script is probably harder to write than a sed-script doing the same and most probably also harder to understand (=less readable). I acknowledge that for one used to awk it is probably the other way round, though.

Here is the sed script doing what you described above:

Code:
sed '/^TLAXIS\// {
               h
               s/^TLAXIS\///
               x
               s/,.*//
    }
    /^GOTO/ {
               G
               s/PT \([ ]*[0-9]*\)\(.*\)/PT\2 \1/
               s/\n//g
    }' /path/to/file

I hope this helps.

bakunin
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #113
Difficulty: Easy
Linux is not Unix per se, but Linux is a Unix-like operating system.
True or False?

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Updated Forum Search Index Min Word Length to 2 Chars and Added Quick Search Bar

Today I changed the forum mysql database to permit 2 letter searches: ft_min_word_len=2 I rebuilt the mysql search indexes as well. Then, I added a "quick search bar" at the top of each page. I have tested this and two letter searches are working; but it's not perfect,... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Sed: how do I insert a \ in my replace

I'm in the process of being forward-thinking and finally converting my site's db to UTF-8. I've already done the UTF-8 conversion (on a copy for testing) and now I want to go through and convert html entities to their actual characters. I ran an entity decode on a mysqldump file but realized... (10 Replies)
Discussion started by: dheian
10 Replies

3. Shell Programming and Scripting

Parse through ~21,000 Database DDL statements -- Fastest way to perform search, replace and insert

Hello All: We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following: 1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON... (5 Replies)
Discussion started by: madhunk
5 Replies

4. Shell Programming and Scripting

Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed. My Existing Text file is Users.txtpaul, Paul Smith, Stevn Smiley, REQ000001, ARS (11) sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11) mike, Mike Conway, Stevn... (8 Replies)
Discussion started by: evrurs
8 Replies

5. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

6. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

7. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

8. Shell Programming and Scripting

Replace Junk chars (Sed)

I know this has been asked previously on this forum...But I think I have a different scenario to present. I ahve a file tht looks like this (note:there are control Z and other chars tht are not visible on line with anme bowers) BB7118450 6004718 BIANCALANA =HEI BZ5842819 ... (4 Replies)
Discussion started by: alfredo123
4 Replies

9. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

sed search and insert

how can i modify a file using sed when searching for a pattern and insert newline after the pattern? pattern example: $ ...(any characters)...$ $ may082001.../tmp/msg.001,,$ REPT CLEAR ALARMS ON UNIT 1 $ may082001.../tmp/msg.002,,$ UNIT 1 IN SERVICE into: $... (1 Reply)
Discussion started by: apalex
1 Replies

Featured Tech Videos