Variables into SED or AWK and multiple commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables into SED or AWK and multiple commands
# 1  
Old 03-19-2012
Question Variables into SED or AWK and multiple commands

Hello I am hoping you may help.

I am not sure how to go about this exactly, I know the tools but not sure how to make them work together.

I have two SED commands that I would like to run in a shell script. I would like to take
the manual input of a user (types in when prompted) to be used as the variables.

Firstly I am not sure how to build the script with cat or loop statements
Secondly how to place a variable inside a SED or NAWK statement so it can be used by those commands.
Thirdly how to produce a temporary file that is date-stamped.

I have a general summary of what I am trying to do below.
#!/usr/bin/
echo "enter name of file" (variable AAAA)
echo "enter first search string1 (variable yyyy)
echo " enter a second search string that searches the file along with string1 ? (variable zzzz)
user types yes, then
enter data
user types no,
then continue with command. sed (filename) variableAAAA > newfilename (newfile with date and time stamp = testfileddmmyyhhmm)
wait 3 seconds
then newfilename sed -e '/./{H;$!d;}' -e 'x;/variableyyyy/'d'
end


Just learning this and hoping you may help.
Thankyou
# 2  
Old 03-19-2012
I guessed a bit, and this code isn't complete, so it should get you started:

Code:
#!/usr/bin/env ksh
# the previous line can be replaced with #!/usr/bin/env bash 
# if you prefer to use bash

printf "enter filename: "
read filename
printf "do you want to enter data [yes|no] "
read ans
case $ans in
    y*|Y*)
            printf "enter search data: "
            read data                       # your description didnt indicate what to do with data
            ;;
    n*|N*)
            ;;              # just continue

    *)      printf "invalid reply; expected Y(es) or N(o)\n"
            exit 1
            ;;
esac

#
#  couldn't tell if this needed to be executed only if user entered no, or in all cases
#
date=$( date +%Y%m%d$H%M%S )                # date for the filename in a variable should it need to be used again
newfilename="testfile.$date"                # complete filename
sed ..... <$filename >$newfilename          # incomplete command, not sure what you need the sed to do
sleep 3
sed   -e '/./{H;$!d;}' -e "x;/$data/d" <$netfilename >not-sure-what-file-for-here



You didn't indicate what to do with the data entered if the user answered 'yes' to the second prompt. I also wasn't sure if a no response was the only time the sed commands were to be executed, or if no was just to avoid entering the data and the sed commands would always be executed.

Last edited by agama; 03-19-2012 at 09:16 PM.. Reason: #! alternate
# 3  
Old 03-19-2012
Bug

Thankyou
Yes sorry, the command you mentioned
sed ..... <$filename >$newfilename # incomplete command, not sure what you need the sed to doI am using a SED command to delete a blank lines in an existing file and it opens a newfile with the new data. I then run it through another SED command which extracts the searched variable(s) that the user inputs. So using

sed '/NZDT/{N;s/\n//;}' access.log > testfile2003121130
then using another SED command with a variable (or two variables) which have been entered by the user (variableyyyy) - putting that in the next SED command and running that on the newfile testfile2003121130. .
so
user inputs first entered data when prompted "john"
user is prompted again if they want to enter another criteria to search at the same time "smith" (search for john smith) - thats not the actual text I am searching for just an example.


sed '/NZDT/{N;s/\n//;}' access.log > testfile2003121130
cat testfile2003121130 | sed -e '/./{H;$!d;}' -e 'x;/variableyyy/!d'
print the result to screen.
I am hoping to automate this rather than put in two separate commands.

Hope that may clarify what I am trying to do.
Thanks again regards
# 4  
Old 03-19-2012
OK, you need to have the user enter a filename, and then a one or two word search string. The file is then searched and all lines matching the search string ('John' or 'John Smith') are extracted and written to a new file tempfile.YYYYMMDD; right?

If that's accurate, then I think this is all you need:

Code:
#!/usr/bin/env ksh
printf "enter filename: "
read filename
printf "enter search data (e.g. john or john smith) "
read search_string
grep "$search_string" $filename >testfile.$(date +%Y%m%d$H%M%S)

# 5  
Old 03-19-2012
Thankyou that description you gave is exactly right, the user enters the filename to be searched followed by the text they wan to search for.

what I found is the SED commands do strip out in the manor I want but I have to run them individually and manually. The first SED command strips out the Blank lines that are in the file which presently separate the access.log individual log data from the date line in the logs.
The second SED command searches for the entered search text that the user enters and strips out the entire paragraph where it is contained.
So I am hoping to run these two in the same script with the user just inputting the filename and search text- else that grep command would be very good .
Any help with this is very much appreciated.
Regards again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use sed commands on multiple lines

I have a text file and i want to run 3 sed commands for the lines entered by the user using perl script. I am doing this manually till now. need some help with this The sed commands I have to use are : sed -i "s/{+//" error.txt sed -i "s/+}//" error.txt sed -i "s/\//g" error.txt... (5 Replies)
Discussion started by: utkarshkhanna44
5 Replies

2. Shell Programming and Scripting

Using sed to execute multiple commands

Let's say I have a file called test.out. In this file I want to do the following: 1. Search for DIP-10219 and with this: 2. Remove everything in front of cn= 3. Remove everything after *com 4. Remove duplicate lines 5. Replace ( with \( 6. Replace ) with \) For 1-3 I have figured out this... (11 Replies)
Discussion started by: exm
11 Replies

3. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

4. Shell Programming and Scripting

Help with passing multiple variables into SED

Hello I am hoping you can help. I use ksh in Solaris9 I am trying to pass user imputed variables into SED but for some reason can only get SED to recognize one variable. I have experimented with te below command with putting ' ' and " " in different places but I cant seem to get it to... (8 Replies)
Discussion started by: lostincashe
8 Replies

5. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

6. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

7. 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

8. Shell Programming and Scripting

Using variables in sed commands

Hi there, I need to be able to put the hostid of my box into a file (replacing the text "enter_hostid_here" so i tried sed -e 's/enter_hostid_here/`hostid`/g' inputfile > outputfile but it takes the `hostid` literally as text .....how can I get this info into the file (ideally in a single... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

9. Shell Programming and Scripting

multiple sed commands

hello! I have a few sed commands sed '/^$/d' < $1 > tmp.t sed '/^ \{3,\}/d' < tmp.t > tmp1.txt ..... how can I write them in a single line? sed '/^$/d' < $1 > | '/^ \{3,\}/d' < $1 > tmp1.txt any idea? thanks. (5 Replies)
Discussion started by: george_
5 Replies

10. Shell Programming and Scripting

sed: Multiple Commands applied to an address

Trying to write a sed command that applies multiple replacements to a specific address. Need a second pair of eyes I guess cause my syntax appears to be correct (obviously not though) I am getting an error. Any Help would be appreciated! Thanks in advance. sed -f foo envOracle sed: Function... (2 Replies)
Discussion started by: google
2 Replies
Login or Register to Ask a Question