LinuxQuestions.org
Visit Jeremy's Blog.
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 04-28-2017, 12:37 PM   #1
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Rep: Reputation: 56
Question 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!
 
Old 04-28-2017, 02:59 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,993

Rep: Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479Reputation: 4479
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/*
 
Old 04-28-2017, 03:05 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
 
Old 04-28-2017, 03:09 PM   #4
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
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!
 
Old 04-28-2017, 03:13 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
 
Old 05-01-2017, 07:55 AM   #6
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
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.
 
Old 05-01-2017, 10:23 AM   #7
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
Question

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!
 
Old 05-01-2017, 10:25 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
Code:
sed '/^ACCT:/s/[0-9][0-9] .... /XXXXXXX/' filename
this should work, but you need to count [0-9] and XXX
 
Old 05-01-2017, 10:42 AM   #9
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
Thank you Pan64; unfortunately, it did not work.
 
Old 05-01-2017, 10:52 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
so please post what did you really try, what's happened, what did you expect.....
 
Old 05-01-2017, 10:54 AM   #11
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
Question

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.
 
Old 05-01-2017, 11:06 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
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.
 
Old 05-01-2017, 11:13 AM   #13
ceantuco
Member
 
Registered: Mar 2008
Location: New York
Distribution: CentOS, Debian & Fedora
Posts: 752

Original Poster
Rep: Reputation: 56
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
 
Old 05-01-2017, 11:14 AM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,229
Blog Entries: 4

Rep: Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260Reputation: 3260
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.
 
Old 05-01-2017, 11:24 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
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
 
  


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 08:55 AM.

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