LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2010, 12:02 PM   #1
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Rep: Reputation: 0
Need help to create a basic search and replace function.


Hello, i have a file of lines that looks like this:

Code:
1          0200000067038 0040209060128
1          0200000069834 0050209060129
1          0200000060350 0040209060129
1          0200000063979 0040209060129
1          0200000064457 0060209060129
1          0200000064464 0030209060130
1          0200000067465 0060209060130

What i need to do, is to remove the space in the middle of the
letters and change the letter at the beginning to 2.
I also need to cut the first spaces down to 4.

When done, it should look like this:

Code:
2    02000000670380040209060128
2    02000000698340050209060129
2    02000000603500040209060129
2    02000000639790040209060129
2    02000000644570060209060129
2    02000000644640030209060130
2    02000000674650060209060130
I haven't done any s&r's before but i see that this could be really
nice to learn.

Help is greatly appreciated and if someone have some guides or
tutorials to recommend on everything from simple to advanced s&r it would be very nice.
 
Old 01-11-2010, 12:14 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Code:
awk '{print 2,"  ",$2 $3}' < f
Where f is the file

Code:
$ cat f
1          0200000067038 0040209060128
1          0200000069834 0050209060129
1          0200000060350 0040209060129
1          0200000063979 0040209060129
1          0200000064457 0060209060129
1          0200000064464 0030209060130
1          0200000067465 0060209060130
$ awk '{print 2,"  ",$2 $3}' < f
2    02000000670380040209060128
2    02000000698340050209060129
2    02000000603500040209060129
2    02000000639790040209060129
2    02000000644570060209060129
2    02000000644640030209060130
2    02000000674650060209060130

Last edited by devnull10; 01-11-2010 at 12:16 PM.
 
Old 01-11-2010, 12:23 PM   #3
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you for a very fast reply!
You just saved me from manually editing thousands of lines like that.

I will definitely look for awk tut's
 
Old 01-11-2010, 12:34 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Not a problem. Bear in mind however that the "2" is hard-coded so if you want to do something different for a number other than 1 (if the case can exist) then you will have to cater for that. If you look up awk then you will be able to find easily how to do this.

Also, you have probably realised, awk writes to stdout, so you need to redirect the output to an appropriate file

Code:
awk '{print 2,"  ",$2 $3}' < f > newfile.txt

Last edited by devnull10; 01-11-2010 at 12:36 PM.
 
Old 01-11-2010, 01:05 PM   #5
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks. In this case, all the lines will start with 2. It may vary per file, but i'll just edit that manually because it's only a few files.
Thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace oliverkazaam41 Linux - General 1 10-29-2009 02:15 AM
Vi Search and Replace mortonmorton Linux - Newbie 6 10-22-2009 09:09 AM
Search and replace TL_CLD Slackware 4 12-25-2007 01:45 PM
search and replace in every file Thinking Programming 3 07-30-2005 05:50 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration