LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-09-2014, 12:10 PM   #1
zbyesk
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
Post Sed using a pattern variable at the start of the line doesn't work


I'm trying to make an script that replaces lines that starts with a pattern given by the lines of other file.

Example lines of the base file:

Code:
& NAME 1
UH   24   1        26.03               1
& NAME 2
UH   27   1        50.05               1
Example lines of the input file:

Code:
UH   24   1        27.68               1
UH   27   1        37.21               1
I just want the script to replace the values (i.ex: 26.03 for 27.68).


I've made the script below but the sed doesn't work.

Code:
while IFS= read -r line
do
    pattern=${line:0:15}
    sed "s/$pattern.*/$line/" BASE.TXT > OUTPUT.TXT
done < INPUT.TXT

Thanks in advance!
 
Old 10-09-2014, 01:46 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
This should do it
Code:
#!/bin/bash
echo "Base file:"
cat BASE.TXT
cp BASE.TXT BASE.TXT.HOLD
while read
	do
		pattern="${REPLY:0:15}"
		sed -i "s/${pattern}.*/$REPLY/" BASE.TXT.HOLD
	done< <(cat INPUT.TXT)
echo
echo
echo "Converted base file:"
cat BASE.TXT.HOLD
echo
You don't need to set IFS, read without a value assigns to the variable REPLY, INPUT.TXT should end in a new line otherwise the last line won't get read in.

After the conversion has been done you can then copy BASE.TXT.HOLD to BASE.TXT if you need to.

Last edited by Keith Hedger; 10-09-2014 at 01:48 PM.
 
  


Reply



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 can i append the text after pattern match using variable in sed ravikushal Linux - Newbie 18 07-23-2013 08:56 AM
[SOLVED] Replace multi line pattern by text variable in sed XXLRay Linux - Software 2 11-22-2012 10:05 AM
sed works on multiple lines but doesn't work on a single line monsteriname Programming 4 10-03-2012 02:23 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
[SOLVED] sed to display the pattern string, the line above it and the first line of that para rockie321 Linux - Newbie 3 04-03-2011 02:48 PM

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

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