Grab text after pattern and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab text after pattern and replace
# 1  
Old 11-04-2015
Grab text after pattern and replace

i have a file which contains data seperated by comma. i want to replace text after 3rd occurrence of a comma.
the input file looks like this

Code:
abcdef,11/02/2015 11:55:47,1001,1234567812345678,12364,,abc
abcdefg,11/02/2015 11:55:47,01,1234567812345678,123,,abc
abcdefhih,11/02/2015 11:55:47,1001,1234567812345678,1234,,abc
abcdef,11/02/2015 11:55:47,001,1234567812345678,1236487,,abc

i want the output to be like

Code:
abcdef,11/02/2015 11:55:47,1001,1234-5678-1234-5678,12364,,abc
abcdefg,11/02/2015 11:55:47,01,1234-5678-1234-5678,123,,abc
abcdefhih,11/02/2015 11:55:47,1001,1234-5678-1234-5678,1234,,abc
abcdef,11/02/2015 11:55:47,001,1234-5678-1234-5678,1236487,,abc

i was able to replace the text with "-" after 4 digits using awk and sed like below but not sure how to get it work within file and then redirect it to a different file

Code:
awk -F',' '{print $4}' file | sed -n -e "s_\(....\)\(....\)\(....\)\(....\)_\1-\2-\3-\4_p"

# 2  
Old 11-04-2015
Hello gpk_newbie,

Could you please try following and let me know if this helps you.
Code:
awk -F, '{gsub(/..../,"&-",$4);sub(/\-$/,X,$4);} 1' OFS=,  Input_file

Output will be as follows.
Code:
abcdef,11/02/2015 11:55:47,1001,1234-5678-1234-5678,12364,,abc
abcdefg,11/02/2015 11:55:47,01,1234-5678-1234-5678,123,,abc
abcdefhih,11/02/2015 11:55:47,1001,1234-5678-1234-5678,1234,,abc
abcdef,11/02/2015 11:55:47,001,1234-5678-1234-5678,1236487,,abc

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-04-2015
Hi RavinderSingh13

thanks a lot that worked as required. Can you pls explain on how it works.
# 4  
Old 11-04-2015
Hello gpk_newbie,

Following may help you to understand the command.
Code:
awk -F,                           ########### Making comma(,) as a field seprator.
'{gsub(/..../,"&-",$4);           ########### using global subtitutaion to change any 4 chars/digits etc to their value with - by using (&-) in $4 as per your request
sub(/\-$/,X,$4);}                 ########### When we use global subtitutaion then it will place - at last of the string then I am removing it by sub which means only single time subtitutaion, That's the difference between global subtitutaion and single substitutaion in awk.
1' OFS=,  Input_file              ########### awk works on condition and then action, if condition is TRUE following action mentioned in it will be performed. So here I am making comndition TRUE by mentioning 1 and no action mentoined so awk will take default action which is print so it will print the complete line then.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 11-04-2015
thanks a lot RavinderSingh13
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 pattern in text

hi unix expert is there any command in linux to repace a pattern in the text to another pattern? many thanks samad (2 Replies)
Discussion started by: abdossamad2003
2 Replies

2. Shell Programming and Scripting

Grab text after pattern on the same line

data: 8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+ 8nwR15UzfeZafaf2bGr8akx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjp lVpOoMLXJ ... (19 Replies)
Discussion started by: SkySmart
19 Replies

3. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

4. Shell Programming and Scripting

Search and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

sed help, Find a pattern, replace it with same text minus leading 0

HI Folks, I'm looking for a solution for this issue. I want to find the Pattern 0/ and replace it with /. I'm just removing the leading zero. I can find the Pattern but it always puts literal value as a replacement. What am I missing?? sed -e s/0\//\//g File1 > File2 edit by... (3 Replies)
Discussion started by: SirHenry1
3 Replies

6. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

7. Shell Programming and Scripting

Replace Text Based On Pattern

Hi I'm trying to replace text in a file based upon a pattern. The pattern I'm looking for is: <styleURL>#style0002</styleURL> <name>#######6105#######</name>The # are seven alphanumeric characters before and after 6105. I need it to replace that with this recursively: ... (4 Replies)
Discussion started by: Grizzly
4 Replies

8. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

9. AIX

Pattern to replace ^M and ^Y in a 4.2 AIX text file

I have files on my AIX 4.2 client system where I need to do the following replacements below but have no clue how ? They are control characters (linefeed, chariage return, ...). First, replace "^M^Y^M" with ^char_for_end_of_line Then replace "^M" with " " Trim all left spaces In VI, my... (7 Replies)
Discussion started by: Browser_ice
7 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question