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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-01-2017, 11:33 AM
|
#16
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
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:
Now, how could I modify the code so it preserves the last 4 digits/characters?
Thank you!
|
|
|
05-01-2017, 11:55 AM
|
#17
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,928
|
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.
|
05-01-2017, 11:58 AM
|
#18
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
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 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
|
|
|
05-01-2017, 12:00 PM
|
#19
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
MadeinGermany,
I posted the response above before I saw your post. I will test it and provide update.
Thank you!
|
|
|
05-01-2017, 12:16 PM
|
#20
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
|
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.
|
|
|
05-01-2017, 01:04 PM
|
#21
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
**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.
|
|
|
05-01-2017, 01:13 PM
|
#22
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
|
Quote:
Originally Posted by pan64
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.™"
|
|
|
05-01-2017, 01:17 PM
|
#23
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Thank you!
|
|
|
05-01-2017, 01:20 PM
|
#24
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
|
Quote:
Originally Posted by ceantuco
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.
|
|
|
05-01-2017, 01:39 PM
|
#25
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
|
Also, with the -i option for GNU sed you can specify a backup file to be made, named with the given suffix. See
|
|
|
05-01-2017, 01:55 PM
|
#26
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Will do! Thank you all for your responses!
|
|
|
05-01-2017, 01:57 PM
|
#27
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
MadeinGermany, Your solution is the one that worked.
Thank you!
|
|
|
05-01-2017, 02:05 PM
|
#28
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,519
|
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///
|
|
|
05-01-2017, 03:53 PM
|
#29
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
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!
|
|
|
All times are GMT -5. The time now is 04:36 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|