LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2018, 12:27 PM   #1
ess2k
LQ Newbie
 
Registered: Feb 2018
Posts: 5

Rep: Reputation: Disabled
Replacing certain characters in line with specific word - Linux script


Hi I am new to linux and am trying to acheive the blow, i have had a play with the sed command but cant get it to do what i need, any help would be appreciated.

I have multiple lines in a similar format, for example,

Code:
#Line a:01:vf:34:56:this.is.a.line.563: Line1
#Line d:32:gt:22:89:this.is.a.line.334: Line2
I would like to identify the line by the ending 'Line1' or 'Line2' and then replace the characters a:01:vf:34:56 for that particular line, no matter what they are, with other characters for example 0:00:00:00:00:

I would like to create a Linux script to do the above.

How would i do this? i have been rsearching, and have come accross the sed command but need help with the finding of the particular line, then selecting and replacing that part of that line.

Thanks

Last edited by ess2k; 02-27-2018 at 12:30 PM.
 
Old 02-27-2018, 12:35 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,098

Rep: Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810
Quote:
Originally Posted by ess2k View Post
Hi I am new to linux and am trying to acheive the blow, i have had a play with the sed command but cant get it to do what i need, any help would be appreciated. I have multiple lines in a similar format, for example,
Code:
#Line a:01:vf:34:56:this.is.a.line.563: Line1
#Line d:32:gt:22:89:this.is.a.line.334: Line2
I would like to identify the line by the ending 'Line1' or 'Line2' and then replace the characters a:01:vf:34:56 for that particular line, no matter what they are, with other characters for example 0:00:00:00:00:

I would like to create a Linux script to do the above. How would i do this? i have been rsearching, and have come accross the sed command but need help with the finding of the particular line, then selecting and replacing that part of that line.
Understand what you want to do, and this can easily be scripted. But you first need to read the "Question Guidelines" link in my posting signature. We're happy to help you, but you first need to show us YOUR EFFORTS. Do this by posting what you have done/tried so far, and what results you got, along with relevant information about your Linux system. We can assist from there, but we aren't going to write your scripts for you.
 
Old 02-27-2018, 12:45 PM   #3
ess2k
LQ Newbie
 
Registered: Feb 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
As stated i am new to linux and have been researching but to no avail, I have been playing with the sed command, and can use it in its simplest form. what i cant seem to do is figure out how to target specific line with a particular word in it, and then i cant figure how to replace only that specific set of characters.

any help or direction would be great.

so can this be done with using just the sed command or do i need to look into another command?

how do i target a line depending on matching text?

how do i replace the characters, since they arent at the beginning or at the end and i dont know how to target them, as suggested by the forum i am posting in, i am a newbie

thanks

Last edited by ess2k; 02-27-2018 at 12:50 PM.
 
Old 02-27-2018, 12:55 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 6,709
Blog Entries: 12

Rep: Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360
You might want to look into the tr(1) as a possible option here for search and replace.

This script you wish to complete would be a combination of:
  1. Process through a number of lines of text data
  2. Search for a string, or perform a replacement. Replacement is either unconditionally performed, or conditionally performed based on the content of the string
  3. Loop
What bash programming experience do you have?

Please share your attempts with sed and describe what you tried and what resulted.
 
Old 02-27-2018, 12:57 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 6,709
Blog Entries: 12

Rep: Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360Reputation: 2360
Moving this to the Programming forum to gain it better exposure. While I realize you chose the Newbie forum because of your limited experience with this, I do feel that the concentration of members who view the Programming forum may be better placed to offer you some suggestions to help you complete this task.
 
Old 02-27-2018, 01:09 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,231

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Quote:
Originally Posted by ess2k View Post
As stated i am new to linux and have been researching but to no avail, I have been playing with the sed command, and can use it in its simplest form. what i cant seem to do is figure out how to target specific line with a particular word in it, and then i cant figure how to replace only that specific set of characters.

any help or direction would be great.

so can this be done with using just the sed command or do i need to look into another command?

how do i target a line depending on matching text?

how do i replace the characters, since they arent at the beginning or at the end and i dont know how to target them, as suggested by the forum i am posting in, i am a newbie

thanks
maybe the regex end-of-line anchor $ mite help ?
also, awk with $NF ?
 
Old 02-27-2018, 01:21 PM   #7
ess2k
LQ Newbie
 
Registered: Feb 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
maybe the regex end-of-line anchor $ mite help ?
also, awk with $NF ?
Can these different commands be used in conjunction with each other?

Thanks
 
Old 02-27-2018, 01:39 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,098

Rep: Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810
Quote:
Originally Posted by ess2k View Post
As stated i am new to linux and have been researching but to no avail, I have been playing with the sed command, and can use it in its simplest form. what i cant seem to do is figure out how to target specific line with a particular word in it, and then i cant figure how to replace only that specific set of characters. any help or direction would be great.
Yes, we realize you're new. As said, "post what you have done/tried".
Quote:
so can this be done with using just the sed command or do i need to look into another command? how do i target a line depending on matching text? how do i replace the characters, since they arent at the beginning or at the end and i dont know how to target them, as suggested by the forum i am posting in, i am a newbie
No, you need to use different commands, leading to...
Quote:
Originally Posted by ess2k
Can these different commands be used in conjunction with each other?
...saying "yes, they can". You can chain commands together in Linux with the pipe ("|") such as
Code:
cat file.txt | grep line | sed 's/li/pi/g' > file1.txt
..which will take file.txt, look for any lines that have the word "line" on them, and replace the li with a pi, and output things to file1.txt. There are LOTS of scripting tutorials you can find, and there's even a link in my posting signature. We realize you're a 'newbie', but again...YOU have to show us your efforts first, and that includes answering questions. You were asked about your Linux system, what you tried already, the results of that effort, and anything relevant. What is your goal, here? Where is that input file coming from? Show us a sample of your desired output.
 
2 members found this post helpful.
Old 02-27-2018, 01:54 PM   #9
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,956

Rep: Reputation: 300Reputation: 300Reputation: 300Reputation: 300
#Line a:01:vf:34:56:this.is.a.line.563: Line1
#Line d:32:gt:22:89:this.is.a.line.334: Line2

I'll give you some examples.

First lets read that into a variable.
Code:
while read line ; do
  text="$text$line\n"
done < <(echo "#Line a:01:vf:34:56:this.is.a.line.563: Line1
            #Line d:32:gt:22:89:this.is.a.line.334: Line2")
Code:
echo -e "$text"
#Line a:01:vf:34:56:this.is.a.line.563: Line1
#Line d:32:gt:22:89:this.is.a.line.334: Line2
Quote:
I would like to identify the line by the ending 'Line1' or 'Line2'
Code:
echo -e "$text" | grep "Line1"
#Line a:01:vf:34:56:this.is.a.line.563: Line1
Quote:
and then replace the characters a:01:vf:34:56 for that particular line,
no matter what they are, with other characters for example 0:00:00:00:00:
Code:
echo -e "$text" | grep "Line1" | sed 's/a:01:vf:34:56:/0:00:00:00:00:/g'
#Line 0:00:00:00:00:this.is.a.line.563: Line1
I suggest you read the man pages for
man sed
man awk
man cut
man grep
 
Old 02-27-2018, 02:10 PM   #10
ess2k
LQ Newbie
 
Registered: Feb 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
thanks for all your help so far,

i have managed to get to do this at the moment,

Code:
sed 's/word1.*word2/word1 new-text word2/' file.txt
This replaces the characters between word1 and word2, Now i just want that to affect only a line that say, ends with the word - Line1

Thanks

Last edited by ess2k; 02-27-2018 at 02:11 PM.
 
Old 02-27-2018, 02:23 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,098

Rep: Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810Reputation: 4810
Quote:
Originally Posted by ess2k View Post
thanks for all your help so far,
i have managed to get to do this at the moment,
Code:
sed 's/word1.*word2/word1 new-text word2/' file.txt
This replaces the characters between word1 and word2, Now i just want that to affect only a line that say, ends with the word - Line1
Both sed and awk are **INCREDIBLY** powerful. More power=more complex...they are both worthy subjects of study.

For example, this:
Code:
sed '/Line1/ s/this/bar/g'
...will replace "this" with "bar", only on lines that contain "Line1". This should get you started, and thanks for showing your own efforts. As said, we're always happy to help, but we're not just going to perform tasks for you without you showing effort.
 
3 members found this post helpful.
Old 02-27-2018, 02:45 PM   #12
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 754

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Following up your idea,
word1 is \w+:\w+:\w+:\w+:\w+ and its substitution is 0:0:0:0:0,
word2 is line1$,
and you mark the to be retained portion by ( ) and put it back as \1 in the substitution string:
Code:
sed -r 's/\w+:\w+:\w+:\w+:\w+(.*Line1)$/0:0:0:0:0\1/' file.txt
Much simpler is a filter (address) with the word2, then it becomes just a substitution of word1:
Code:
sed -r '/Line1$/ s/\w+:\w+:\w+:\w+:\w+/0:0:0:0:0/' file.txt
 
Old 02-27-2018, 03:19 PM   #13
ess2k
LQ Newbie
 
Registered: Feb 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you very much,

this has done the trick,

Code:
sed '/Line1/ s/this/bar/g' file.txt
I have even managed to consolidate 2 commands into one by

Code:
sed '/Line1/ s/this/bar/g;/Line2/ s/that/far/g' file.txt


Thanks all for help and direction.
 
1 members found this post helpful.
  


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
replacing a column in specific line bash script Elaheh_se Programming 3 09-30-2016 01:40 PM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
[SOLVED] Bash script to parse a file to get a set of line between a specific characters venkatrg Linux - Newbie 5 12-24-2010 06:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration