How to identify particular section using sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to identify particular section using sed?
# 1  
Old 03-11-2014
How to identify particular section using sed?

Hi, I have following data in a file. Not all but most of the lines start with letter 'T' has 8 SPACES from column 121 to 128 and I want to replace that portion with some dummy value. Is it possible through sed?

Input File:-
Code:
 
T1111111111111A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S1111111111111A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T2222222222222A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S2222222222222A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T3333333333333A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S3333333333333A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T4444444444444A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S4444444444444A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T5555555555555A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S5555555555555A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T6666666666666A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S6666666666666A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T7777777777777A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S7777777777777A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T8888888888888A       20140310000000005076358416369283  AAAAA                    BBBBBBB                2014010220140102        A0100        1              507635    P201401021325        1325        A     000003
S8888888888888A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T9999999999999A       20140310000000005070898414104282  AAAA                     BBBBBB                 201401222014012220140205             1              507089    P201401221450        1450        A     000003
S9999999999999A       XXQWERTYUIO                     WI7909 W CONGRESS ST APT 1      QWERTYUIO                     WI53218    NA  3070 V DFGH ST STE 101        QWERTYUIO                     WI53210    NA  3

Output file:-
Code:
T1111111111111A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S1111111111111A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T2222222222222A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S2222222222222A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T3333333333333A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S3333333333333A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T4444444444444A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S4444444444444A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T5555555555555A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S5555555555555A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T6666666666666A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S6666666666666A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T7777777777777A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S7777777777777A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T8888888888888A       20140310000000005076358416369283  AAAAA                    BBBBBBB                201401022014010220140331A0100        1              507635    P201401021325        1325        A     000003
S8888888888888A       XXQWERTY                        WIN2451 ABCDE RD TRLR 17        QWERTY                        WI53566    NA  515 PQRS AVE                  QWERTY                        WI53566    NA  7    
T9999999999999A       20140310000000005070898414104282  AAAA                     BBBBBB                 201401222014012220140205             1              507089    P201401221450        1450        A     000003
S9999999999999A       XXQWERTYUIO                     WI7909 W CONGRESS ST APT 1      QWERTYUIO                     WI53218    NA  3070 V DFGH ST STE 101        QWERTYUIO                     WI53210    NA  3

# 2  
Old 03-11-2014
Quote:
start with letter 'T' has 8 SPACES from column 121 to 128 and I want to replace that portion with some dummy value. Is it possible through sed?
What makes you think it not possible with sed?
# 3  
Old 03-11-2014
I am not sure how can we identify a particular portion of a line using sed and replace it. So I am not sure if it is possible with sed.

Last edited by jnrohit2k; 03-11-2014 at 02:53 PM..
# 4  
Old 03-11-2014
This is based on the pattern in the input file

Code:
sed '/^T/{s/\([0-9]\)        \([A-Z]\)/\120140331\2/}' infile

# 5  
Old 03-11-2014
Based on the position in the line, if the line starts with "T" and there are 8 space from pos.121:
Code:
sed 's/^T\(.\{119\}\) \{8\}/T\120140331/' file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-11-2014
It worked. Can you please explain why you used 119 and what curly '{' braces do?

Thanks!
# 7  
Old 03-11-2014
T + 119 arbitrary characters = 120. The curly braces with the back slash ( \{ and \} are interval expressions, they repeat the part before it. So .\{119\} means 119 consecutive arbitrary characters.. and \{8\} means 8 spaces.

Last edited by Scrutinizer; 03-12-2014 at 09:50 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to identify delimiter to find and replace a string with sed?

I need to find and replace a date format in a SQL script with sed. The original lines are like this: ep.begin_date, ep.end_date, ep.facility_code, AND ep.begin_date <= '01-JUL-2019' ep.begin_date, ep.end_date, ep.facility_code, AND ... (15 Replies)
Discussion started by: duke0001
15 Replies

2. Shell Programming and Scripting

How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text. Here is what I have so far that isn't working all that well for me. Any help would be... (7 Replies)
Discussion started by: jbrass
7 Replies

3. Shell Programming and Scripting

Section Removal With sed; and With a Twist . . .

Hello folks! Raised a bump on my head trying to figure this one out ;) I have an xml file which needs to be edited, removing an entire property section in the work. Here's what the target section layout looks like: <property name="something"> {any number of lines go here} </property>... (7 Replies)
Discussion started by: LinQ
7 Replies

4. Shell Programming and Scripting

sed - String substitution within specified section in ini type file

Hello. I am trying to modify a config file which is in windows *.ini type file. I have found a piece of code here :linux - Edit file in unix using SED - Stack Overflow As I can't make it doing the job , I am trying to find a solution step by step. here a modified sample file : my_sample.ini... (1 Reply)
Discussion started by: jcdole
1 Replies

5. Shell Programming and Scripting

Prepend first line of section to each line until the next section header

I have searched in a variety of ways in a variety of places but have come up empty. I would like to prepend a portion of a section header to each following line until the next section header. I have been using sed for most things up until now but I'd go for a solution in just about anything--... (7 Replies)
Discussion started by: pagrus
7 Replies

6. Shell Programming and Scripting

Help identify string using sed

I have the following output and would like to only identify strings with "vw" at the end. Here is the file contents: SELECT n.contract_num, n.descr, s.prj_level2_cf_val, r.descr, r.project_id, p.offering_id, o.n_cust_contract, u.name1, ' ', ' ', SUM (0), TO_CHAR (t.start_dt, 'YYYY-MM-DD'),... (6 Replies)
Discussion started by: bobroberts369
6 Replies

7. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

8. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies

9. Shell Programming and Scripting

Using Sed to duplicate a section of a file....

hello all, I have a file like this: section 1 blah1 blah2 section 2 blah1 blah2 section 3 blah1 blah2 and I want to use sed to duplicate section 2, like this: section 1 blah1 blah2 section 2 blah1 blah2 section 2 blah1 (2 Replies)
Discussion started by: nick26
2 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question