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.
|
|
|
04-28-2017, 12:37 PM
|
#1
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Rep:
|
Replace string in several files at position
Hi Everyone,
I have over 35k files and I would like to replace the first 12 characters of certain string on all files with 'x'.
I used grep -ir 'string_name' /home/user/files/* to list the files that contain the specific string I want to change. How can I use SED or AWK to accomplish this task?
Please advise
Thank you!
|
|
|
04-28-2017, 02:59 PM
|
#2
|
LQ Addict
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,159
Rep:
|
Lets set A as the initial string and B as the modified string.
If you use GNU sed, write:
Code:
sed -i s/$A/$B/g /home/user/files/*
|
|
|
04-28-2017, 03:05 PM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,507
|
Which first twelve? Can you describe your pattern a little more?
I'd anchor the pattern to the beginning of the line so that matches only the first letters not anywhere else on the line. Leaving off the /g modifier also restricts the pattern to a single attempt per line:
Code:
sed -e "s/^$A/$B/;" /path/to/files/*.txt
And be sure to leave off the -i until you are sure the pattern works.
See:
Code:
man sed
man 7 regex
Also, it is best if you have a backup using tar before experimenting.
|
|
|
04-28-2017, 03:09 PM
|
#4
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Thank you for your responses!
Okay so basically each file contain x number of entries as shown below:
Acct: 1111111111111111
and I would like to replace all entries on all files as follows:
Acct: XXXXXXXXXXXX1111
Yes, I will back up the data before I start experimenting. Wouldn't want to destroy all information within those files.
Thank you!
|
|
|
04-28-2017, 03:13 PM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,507
|
I'm not sure about which pattern you want to zap but if you want to replace the first twelve letters of all lines, then the following would do it:
Code:
sed -r -e 's/^.{12}/xxxxxxxxxxxx/' *.txt
If you want a more precise search pattern, then replace .{12} with a more detailed pattern.
|
|
|
05-01-2017, 07:55 AM
|
#6
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Thank you Turbocapitalist. I will give your suggestion above a try. If it does not work, I will have to be more specific with what I would like to accomplish.
|
|
|
05-01-2017, 10:23 AM
|
#7
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
I tried what suggested but it did not work. Basically, my files contain many lines of text which some of these lines start with:
Code:
ACCT: 3478913473413536
which I would like to change to:
Code:
ACCT: XXXXXXXXXXXX3536
I tried the code below to test and I was able to locate the line where "Acct: " is present and replace it with "Cess: "
Code:
grep -irl "ACCT: " file.txt | xargs sed -i 's/ACCT: /Cess: /gI'
What I need is a away to tell SED to replace the first 12 digits after "ACCT: " with "X"
Hopefully the explanation above is easier to understand.
Thank you!
|
|
|
05-01-2017, 10:25 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
Code:
sed '/^ACCT:/s/[0-9][0-9] .... /XXXXXXX/' filename
this should work, but you need to count [0-9] and XXX
|
|
|
05-01-2017, 10:42 AM
|
#9
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Thank you Pan64; unfortunately, it did not work.
|
|
|
05-01-2017, 10:52 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
so please post what did you really try, what's happened, what did you expect.....
|
|
|
05-01-2017, 10:54 AM
|
#11
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
Of course; I tried the command below:
Code:
sed '/^ACCT:/s/[0-9][0-9] .... /XXXXXXX/' filename
Then I opened the file to see if the string(s) has been changed but they were not:
Code:
ACCT: 3478913473413536
I expected:
Code:
ACCT: XXXXXXXXXXXX3536
Thank you
Last edited by ceantuco; 05-01-2017 at 10:55 AM.
|
|
|
05-01-2017, 11:06 AM
|
#12
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
of course, you need to repeat [0-9] 12 times, if you want to replace 12 digits. or course you must not add ..... and spaces.
of course, you need to specify the replacement string exactly too.
|
|
|
05-01-2017, 11:13 AM
|
#13
|
Member
Registered: Mar 2008
Location: New York
Distribution: Debian
Posts: 809
Original Poster
Rep:
|
I was not trying to be sarcastic with 'of course'.... no need to repeat it x times.
would this work?
Code:
sed '/^ACCT:/s/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ACCT: XXXXXXXXXXXX/' filename
|
|
|
05-01-2017, 11:14 AM
|
#14
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,834
|
s/^.d{12}/xxxxxxxxxxxx/ ...?
I mean this to be an expression which replaces "12 occurrences of 'anything', anchored at the start of the string," with 12 x's.
|
|
|
05-01-2017, 11:24 AM
|
#15
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
try that, and you will see. but I would really like to ask you to understand/learn how sed works:
Code:
/^ACCT:/ look for lines beginning with ACCT:
s means the command substitution
/ delimiter
something to search for
/ delimiter
string replacement string
/ closing delimiter
optional modifiers, at this time there is no any
from this point of view probably the replacement string is not ok (what you specified), but you need to know that exactly
{12} works only with sed -r
|
|
|
All times are GMT -5. The time now is 04:29 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
|
|