Find, Replace & Edit a string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find, Replace & Edit a string?
# 1  
Old 04-20-2009
Question Find, Replace & Edit a string?

Is this something SED would be used for or can AWK do it?

I have a string that I would like to chop bits out of and re-arrange some of the rest.

Basically I want to change this:
Code:
<log4j:event logger="webserver" timestamp="1240110840109" time="Sun Apr 19 04:14:00 BST 2009" level="INFO" thread="webserverThread-0:0" schema="LMS">

to this:
Code:
14:00,19-04-2009

Thanks.

Last edited by Yogesh Sawant; 04-20-2009 at 10:47 AM.. Reason: added code tags
# 2  
Old 04-21-2009
I guess AWK should do it. If the fields are fixed then just print them.
Something like this

echo "<log4j:event.........schema="LMS">" | awk '{ print $3,$4, substr($5,3,5), $5...'}
Here $3, $4 prints te whole word/field and substr($5,3,5) prints 5 characters of 5th field starting from the 3rd letter.

Modify the awk with your requirements.

Thanks and Regards,
Gideon.
# 3  
Old 04-29-2009
Thanks for that!

Not sure how I can go about changing the Apr into a 4 and Jan into a 1 and so on...

Any ideas?
# 4  
Old 04-29-2009
I don't know if it's the best way, but I'd setup a script with a line like:
monthnum=0
for month in (Jan Feb Mar Apr); do
monthnum=$(($monthnum+1))
sed -e "s/'$month'/'$monthnum/" <infile >outfile
done

Thats off the top of my head, obviously you'll need to write the whole script
# 5  
Old 04-30-2009
Thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

2. UNIX for Dummies Questions & Answers

Find & Replace

Hi I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense CDR.20060505.150006.gb CDR.20121211.191500.gb... (3 Replies)
Discussion started by: rob171171
3 Replies

3. Shell Programming and Scripting

LINUX - How to replace a string & its current value with a new value

Hi All, i am trying to replace a string & its current value (P_JOBID =1000 for eg) in a file with a new value(P_JOBID =2000) through sed command. File name : A.txt Command I am trying : `cat $path/A.txt | grep ${P_JOBID} | sed -e s/P_JOBID=/P_JOBID="$JobID"/g` Output Expected :... (5 Replies)
Discussion started by: dsfreddie
5 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

6. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

7. UNIX for Dummies Questions & Answers

vi search & replace ... having '/' in string - problem.

I want to carry out search & replace for the paths mentioned in the file with the help of vi. 'abc/' to be replaced by 'abc/data' When I use command in vi as below - %s/abc//abc/data/g it gives me an error. How we should deal with '/' part in string for vi search & replace? ... (6 Replies)
Discussion started by: videsh77
6 Replies

8. UNIX for Dummies Questions & Answers

String Search & Replace

Hey, I want to have a C program which, for an existing file supplied by the command line argument (E.g. File1.txt) replaces all the occurrences of the words: "We” or “we” by “I” “a” by “the” “A” by “The”. Then print the replaced file. All other characters of the file are to be left... (1 Reply)
Discussion started by: IwishIknewC
1 Replies

9. UNIX for Dummies Questions & Answers

improving my script (find & replace)

Hi all, I have a script that scan files, find old templet and replace it with new one. #!/bin/ksh file_name=$1 old_templet=$2 new_templet=$3 # Loop through every file like this for file in file_name do cat $file | sed "s/old_templet/new_templet/g" > $file.new #do a global searce and... (8 Replies)
Discussion started by: amir_yosha
8 Replies

10. Shell Programming and Scripting

Find & Replace

I get a text file with 70+ columns (seperated by Tab) and about 10000 rows. The 58th Column is all numbers. But sometimes 58th columns has "/xxx=##" after the numeric data. I want to truncate this string using the script. Any Ideas...:confused: (3 Replies)
Discussion started by: gagansharma
3 Replies
Login or Register to Ask a Question