LinuxQuestions.org
Help answer threads with 0 replies.
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 05-01-2017, 11:33 AM   #16
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88

I tried it but it did not replace anything.

I tried the command below:

Code:
sed -i 's/\(  ACCT: \)[^ ]*/\1XXXXXXXXXXXX/' filename
and the string changed from:

Code:
  ACCT: 3478913473413536
to:

Code:
  ACCT: XXXXXXXXXXXX
Now, how could I modify the code so it preserves the last 4 digits/characters?

Thank you!
 
Old 05-01-2017, 11:55 AM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,928

Rep: Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247
The following cuts ACCT: surrounded by space, and puts it back via a back-reference.
The following 12 digits are substituted by 12 X
Code:
sed 's/^\([[:space:]]*ACCT:[[:space:]]*\)[0-9]\{12\}/\1XXXXXXXXXXXX/' filename

Last edited by MadeInGermany; 05-01-2017 at 11:58 AM.
 
1 members found this post helpful.
Old 05-01-2017, 11:58 AM   #18
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
Question

After playing with the command above, I tried the command below:

Code:
sed -i 's/\(  ACCT: \)[^0 ]*/\1XXXXXXXXXXXX/' filename
and the string changed from:

Code:
  ACCT: 3478913473413536
to:

Code:
  ACCT: XXXXXXXXXXXX413536
I tried replacing the
Code:
^0
with numbers from 1-10; however, it does not produce the desired output of:

Code:
  ACCT: XXXXXXXXXXXX3536
I think I am close to the solution but I don't know what else to try/change.

Thank you
 
Old 05-01-2017, 12:00 PM   #19
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
MadeinGermany,

I posted the response above before I saw your post. I will test it and provide update.

Thank you!
 
Old 05-01-2017, 12:16 PM   #20
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
Blog Entries: 4

Rep: Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829
It is also important that you get to the point where you can build or extend regular expressions on your own. One, it will save you effort. Two, you encounter them everywhere these days.

When you have the working solution, which I think you will in the next few minutes, try looking at the manual page already mentioned and working through this tutorial: http://www.regular-expressions.info/quickstart.html to analyze the components of the pattern used.
 
Old 05-01-2017, 01:04 PM   #21
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
Question

**CORRECTION**

MADEINGERMANY, Thank you so much! the solution you provided worked like a charm! yes, there are other expressions with less characters in the string that I might need to also modify so I will play with the solution you provided to change those expressions.

Since I have thousands of files I need to modify and they are located in multiple sub folders within a folder, should I run the code as shown below?

Code:
sed -i -r 's/^\([[:space:]]*ACCT:[[:space:]]*\)[0-9]\{12\}/\1XXXXXXXXXXXX/' /home/user/folder/*
Thank you

Last edited by ceantuco; 05-01-2017 at 01:56 PM.
 
Old 05-01-2017, 01:13 PM   #22
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
Blog Entries: 4

Rep: Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995
Quote:
Originally Posted by pan64 View Post
Code:
/^ACCT:/   look for lines beginning with ACCT:
... and, to illustrate just how subtle regular-expressions ("regexes") can be, it is the "^" character which says, "anchor to start-of-line." (Similarly, "$" anchors to end-of-line.)

There are many regex tester web-sites out there which are frankly worth their weight in gold. They let you type in a regex, and a test string, and they will actually show you the logic that is applied to obtain a "match" or "doesn't match" result. They will also point out any syntax-errors in your regex. "Don't leave home without it.™"
 
Old 05-01-2017, 01:17 PM   #23
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
Thank you!
 
Old 05-01-2017, 01:20 PM   #24
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
Blog Entries: 4

Rep: Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995Reputation: 3995
Quote:
Originally Posted by ceantuco View Post
Since I have thousands of files I need to modify and they are located in multiple sub folders within a folder, should I run the code as shown below?
Personally, I use the find command to produce a list of the files that it considers to be "matches." Then, I e-y-e-b-a-l-l that list ... several times. (I'm only gonna get one chance at this ...)

Finally, I apply that list, e.g.:

cat listfile | xargs -I{} mycommand "{}"

(Notice the use of "quotes" in the substituted command, to handle the case of a path-string that contains spaces.)

Sometimes I'll start by executing [font=courier]echo commands that echo the command-strings I am about to execute. Sometimes, I'll capture that echo-output into a file, give it one... last... look..., then execute that file.

That degree of caution has saved my left foot many times.
 
Old 05-01-2017, 01:39 PM   #25
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
Blog Entries: 4

Rep: Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829
Also, with the -i option for GNU sed you can specify a backup file to be made, named with the given suffix. See
Code:
man sed
 
Old 05-01-2017, 01:55 PM   #26
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
Will do! Thank you all for your responses!
 
Old 05-01-2017, 01:57 PM   #27
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
MadeinGermany, Your solution is the one that worked.
Thank you!
 
Old 05-01-2017, 02:05 PM   #28
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
Blog Entries: 4

Rep: Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829Reputation: 3829
Be sure to look up what options -i and -r do. The parenthesis and braces have to be written normally if you use -r. And -i usually takes some string with it.

Code:
sed -r 's/^([[:space:]]*ACCT:[[:space:]]*)[0-9]{12}/\1XXXXXXXXXXXX/' /home/user/folder/*
Also check the references mentioned for explanations of all the components in the regular expression:

^, ( ), [ ] as in [0-9], [:space:], *, and {12}

In the replacement lookup \1

Same for the command s///
 
Old 05-01-2017, 03:53 PM   #29
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809

Original Poster
Rep: Reputation: 88
Thank you! I was able to modify the command to be able to change other strings in the same file which contain less/more characters and they all worked okay. I will def read up on 'sed' and the references mentioned above. I will also do more testing before changing all files. Thank you again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to use sed replace matching string of line contents at fixed position lwsunny Linux - General 5 07-31-2013 08:50 PM
how to replace occurrence of a string in many files in one command tkmsr Programming 1 10-30-2010 06:29 AM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM
Search and replace string in executable files aalex77 Programming 2 05-26-2006 06:32 PM

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

All times are GMT -5. The time now is 04:36 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