using sed to find a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed to find a pattern
# 1  
Old 04-25-2012
using sed to find a pattern

Hi All,

I have a file with 100 lines contain a pattern like:

input:
Code:
a,b,4,2343,56456,7656,78787,786234,00:59:16.740 04 17 2012,4564,2453,556
f,j,er,45,fh,67,00:59:45.840 04 25 2012, sdf,grty,rtyr,wqerw

output
Code:
a,b,4,2343,56456,7656,78787,786234,00:59:16.7 04172012,4564,2453,556
f,j,er,45,fh,67,00:59:45.8 04252012, sdf,grty,rtyr,wqerw

1. 40 was cut 00:59:16.740 -> 00:59:16.7
2. space was removed 04 17 2012 -> 04172012

Could anyone help?

Moderator's Comments:
Mod Comment Link: How to use [code] tags

Last edited by Scrutinizer; 04-25-2012 at 03:07 AM..
# 2  
Old 04-25-2012
Try this:

Code:
sed  -r 's/ ([0-9][0-9]) ([0-9][0-9]) ([0-9][0-9][0-9][0-9])/ \1\2\3/; s/\.([0-9])([0-9])([0-9])/.\1/ ' input-file >output-file



It will change the first of each pattern on each record.

If you are using a BSD style sed, use -E instead of -r.

Last edited by agama; 04-25-2012 at 12:44 AM.. Reason: additional thought
# 3  
Old 04-25-2012
Quote:
Originally Posted by agama
Try this:

Code:
sed  -r 's/ ([0-9][0-9]) ([0-9][0-9]) ([0-9][0-9][0-9][0-9])/ \1\2\3/; s/\.([0-9])([0-9])([0-9])/.\1/ ' input-file >output-file



It will change the first of each pattern on each record.

If you are using a BSD style sed, use -E instead of -r.
Hi Agama,

It get error - sed: command garbled: s/ ([0-9][0-9]) ([0-9][0-9]) ([0-9][0-9][0-9][0-9])/ \1\2\3/

Could you please help?
# 4  
Old 04-25-2012
please use code tag

https://www.unix.com/how-post-unix-li...code-tags.html

the input line which you provided in the first post is 2 lines or 1 line ?
# 5  
Old 04-25-2012
2 lines
# 6  
Old 04-25-2012
if it is same line, then you can try this.

Code:
 
$ nawk -F, -v OFS=, '{split($9,arr," ");len=length(arr[1]);$9=substr(arr[1],1,(len-2))" "arr[2]""arr[3]""arr[4];print}' input.txt                   
a,b,4,2343,56456,7656,78787,786234,00:59:16.7 04172012,4564,2453,556f,j,er,45,fh,67,00:59:45.840 04 25 2012, sdf,grty,rtyr,wqerw

---------- Post updated at 10:50 AM ---------- Previous update was at 10:48 AM ----------

is this time and date values comes in different position in all the lines ?

or in the same position ?

00:59:16.740 04 17 2012

in the posted example, date and time comes in 9th position in the first line.
but in the second line, it comes in the 7th position
# 7  
Old 04-25-2012
HI itkamaraj,

Thanks for yours help. It's work. But how can the result output in a file?

Thanks,
Mimi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
6 Replies

2. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

6. Shell Programming and Scripting

sed to find pattern and delete line before and after

I have the following file: line1 line2 MATCH line3 line4 I need to find the pattern, "MATCH" and delete the line before and after MATCH. So the result should be line1 MATCH lline4 I have to use sed because it is the only utility that is common across my environments. I... (1 Reply)
Discussion started by: craftereric
1 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. UNIX for Dummies Questions & Answers

sed to find a pattern

Hi, I am having a file named old with contents as below, Tea Time Sea Tie Team if I use something like cat old | grep "^T", it use to print Tea Time Tie Team Question, Plz hlp? But how will I print a a line that matches these criteria and Was exactly three letters long...so that... (2 Replies)
Discussion started by: shyjuezy
2 Replies

9. Shell Programming and Scripting

Find a pattern and replace using sed.

Hi I need to help on finding the below pattern using sed <b><a href="/home/document.do?assetkey=x-y-abcde-1&searchclause=photo"> and replace as below in the same line on the index file. <b><a href="/abcde.html"> thx in advance. Mari (5 Replies)
Discussion started by: maridhasan
5 Replies

10. Shell Programming and Scripting

grep and sed to find a pattern and add newline

Hello All, I have log file the result from a multithreaded process. So when a process finishes it will write to this log file as 123 rows merged. The issue is sometimes the processess finish at the same time or write to the file at the same time as 123 rows merged.145 rows merged. At... (5 Replies)
Discussion started by: ssikhar
5 Replies
Login or Register to Ask a Question