using sed on bash variables (or maybe awk?)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed on bash variables (or maybe awk?)
# 1  
Old 03-13-2008
using sed on bash variables (or maybe awk?)

Hi all-

I've been fooling with this for a few days, but I'm rather new at this...

I have a bash variable containing a long string of various characters, for instance:

JUNK=this that the other xyz 1234 56 789

I don't know what "xyz" actually is, but I know that:
START=he other
and
STOP=123
In other words, "he other" and "123" are kind of delimiters for whatever "xyz" is.

So, how can I feed sed (or maybe awk?) the variables $JUNK, $START and $STOP to extract "xyz" (and assign it to another variable) without knowing what "xyz" is?

Any hints would be great!

-Rev66
# 2  
Old 03-13-2008
Quote:
Originally Posted by rev66
Hi all-

I've been fooling with this for a few days, but I'm rather new at this...

I have a bash variable containing a long string of various characters, for instance:

JUNK=this that the other xyz 1234 56 789

I don't know what "xyz" actually is, but I know that:
START=he other
and
STOP=123
In other words, "he other" and "123" are kind of delimiters for whatever "xyz" is.

So, how can I feed sed (or maybe awk?) the variables $JUNK, $START and $STOP to extract "xyz" (and assign it to another variable) without knowing what "xyz" is?

You don't need ''sed'' or ''awk''; the shell has parameter expansion that is much faster than calling an external utility.
Code:
JUNK="this that the other xyz 1234 56 789"
temp=${JUNK#*he other }
extract=${temp% 123*}

Read the "Parameter Expansion" section of your shell man page for more infromation.
# 3  
Old 03-14-2008
Quote:
Originally Posted by cfajohnson
[INDENT]
You don't need ''sed'' or ''awk''; the shell has parameter expansion that is much faster than calling an external utility.
for a simple case like OP's, true. however if it comes from a file, especially if its big file, it might be faster to use awk/sed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending awk variables into curl in a bash script

Hello experts! I have a file1 with the following format (yr,day, month, hour,minute): 201201132435 201202141210 201304132030 201410100110 ... What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page. What I have... (6 Replies)
Discussion started by: phaethon
6 Replies

2. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 0 1056 85540 414329 774485 1208487 1657519 2102753 2561259 3037737 3458144 3993019 4417959 4809964 5261890 5798778 6254146 I want to find all lines between a specified start and end tag. (6 Replies)
Discussion started by: jamie_123
6 Replies

3. Shell Programming and Scripting

Updating variables using sed or awk

Hi, I have a file(testfile.txt) that contains list of variables as shown below. T $$FirstName=James $$LastName=Fox $$Dateofbirth=1980-02-04 ……and so on there are 50 different variables. I am writing a script(script1.sh) that will update the above three variable one by one with the values... (6 Replies)
Discussion started by: Saanvi1
6 Replies

4. Debian

Using sed with bash variables

Hi Guys I have another problem I'm trying to solve and hope that some one can help me here. This is the scenario: I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE. Below is the bash script I'm... (6 Replies)
Discussion started by: linuxjunkie
6 Replies

5. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

6. Shell Programming and Scripting

Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables. for i in $(seq 30 5 60) do # Define variables up and down in AWK eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null) eval $(awk 'BEGIN{ print... (2 Replies)
Discussion started by: lily-anne
2 Replies

7. UNIX for Dummies Questions & Answers

Use of Variables in a sed/awk script

Hi, After looking at the differents post on this forum, I am convinced that I will benefit from the experience of advanced Unix user on some script I have already done for an aeronautical study. Here is one of them : Step 1 : sed -e "s/??/00/g" Base_Awk.txt > Awk_Cut_00.txt4; sed... (11 Replies)
Discussion started by: Marc_Camoc
11 Replies

8. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

9. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

10. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies
Login or Register to Ask a Question