LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-11-2012, 11:07 AM   #1
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Rep: Reputation: Disabled
Take part of a Linux command line from a file


I have some scripts. Each script compiles a different library and each script uses a sed string to modify the output.

e.g. gmake library | sed 's/powerpc.*[ \t]/compiling -->/'

I would like to make all scripts use the same sed string from a file so I don't have to modify each script everytime I change the sed string.

I tried putting everything after the | symbol into a file called sed_string then I tried using "<sed_string" but, as expected, that did not work.

Does anyone know how to do this?
 
Old 01-11-2012, 11:17 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Suggestion one:

1) Create a function for your sed command and modify the scripts so that they use it.

2) Store the function in a separate text file.

3) Source that file into your scripts.


Suggestion two:

Use sed's ability to read expressions from a file.

Code:
sed -f filename
 
1 members found this post helpful.
Old 01-11-2012, 11:54 AM   #3
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Original Poster
Rep: Reputation: Disabled
I don't know how to make sed functions so I tried the -f method. It worked perfectly.

Thanks
 
Old 01-11-2012, 04:16 PM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,763

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
ed

IN that case might you want to mark this thread SOLVED?
 
Old 01-12-2012, 06:26 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not a sed function, a shell function, containing the sed command you want. sed itself doesn't have a function feature.

The details depend to some extent on which shell you use, but if you're using bash:

http://mywiki.wooledge.org/BashGuide...ands#Functions
http://www.tldp.org/LDP/abs/html/functions.html

And see here for some details about sourcing a file:

http://wiki.bash-hackers.org/howto/conffile
 
Old 01-12-2012, 06:50 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,927
Blog Entries: 45

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 01-13-2012, 10:14 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your problem can be decomposed into two smaller problems, each with solutions.

1. You want to read stuff from a file. This reads one line from a file called sed_string, into a variable called $argVar
Code:
read argVar < sed_string
# Echo the line, just to make sure...
echo $argVar
2. You can use the variable as a commandline argument to sed. If the line read from the file might contain whitespace, you should enclose it in quotes.
Code:
sed \'${argVar}\'
You can combine these component code fragments in loops and larger code segments according to your requirements.

--- rod.
 
Old 01-13-2012, 11:39 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
sed \'${argVar}\'
This quoting style doesn't work:

Code:
$ echo $argVar
s/foo bar/foobar/
$ echo 'foo bar' | sed \'${argVar}\'
sed: -e expression #1, char 1: unknown command: `''
$ echo 'foo bar' | sed "${argVar}"
foobar
 
1 members found this post helpful.
Old 01-13-2012, 12:46 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Duhhh. I only tested the quoting with an echo command. Thanks for the catch.

--- rod.
 
Old 01-14-2012, 01:12 AM   #10
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by theNbomr View Post
2. You can use the variable as a commandline argument to sed. If the line read from the file might contain whitespace, you should enclose it in quotes.
Code:
sed "${argVar}"
I agree. (theNbomr, I took the liberty to fix the quotes there).

Additionally, eddyq, if you use a Bash shell, you can compose the arguments to the command in an array first, then supply the array as the command-line parameters.

For example, you might wish to use a sed command, which takes the regular expressions from one source, edit mode options from another, and file names from third source:
Code:
#!/bin/bash

# Keep all non-ASCII characters intact, regardless of the charset.
export LANG=C LC_ALL=C

# This will contain the command line arguments for sed.
# Note that "${args[@]}" means "each separate item in args".
args=()

# Add a pattern that changes each 'foo' to 'bar'.
args=("${args[@]}" -e 's|foo|bar|g')

# Add a pattern which removes lines that begin with a # or a ;.
args=("${args[@]}" -e '/^[\t\v\f\r ]*#/ d')

# Add option -i which tells sed to edit the target files in-place,
# instead of outputting the changes to standard output.
args=("${args[@]}" -i)

# Add everything supplied on the command line to this script, too.
# (Basically, assume they're all file names or sed options.)
args=("${args[@]}" "$@")

# Execute the sed command, using parameters defined above.
sed "${args[@]}"
As you can see, this is quite easy to use, and allows you to construct very complex commands without relying on tedious if-then or case-esac structures.

Do note, however, that input and output redirections ( <input-file and >output-file ) are handled by the shell, not the command itself, so you cannot do redirections by inserting those into the array.
 
  


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
LXer: Command Line Magic: Scripting, Part One LXer Syndicated Linux News 0 12-08-2011 09:20 PM
LXer: An Introduction To Linux Command Line Part-1 LXer Syndicated Linux News 0 08-07-2011 01:20 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
LXer: Switch to the Command Line, Part 1. LXer Syndicated Linux News 1 05-13-2009 04:07 AM
LXer: Powerful Multimedia Command-Line Tools, Part I - SoX LXer Syndicated Linux News 0 11-30-2007 10:10 PM

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

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