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 10-09-2014, 01: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, 02:46 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,480

Rep: Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620
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 02:48 PM.
 
  


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

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

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