Removing multiple values from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing multiple values from a variable
# 1  
Old 05-29-2013
Removing multiple values from a variable

Hi
I have a requirement like
Code:
# e=`ls | grep -e"test" | awk -F " " '{print $1}'`

(0) root @ ptxfv3: /
# echo $e
test test.xml

From this i need to grep the word "test" alone i.e., it is occuring twice I need only one among them

Please help
# 2  
Old 05-29-2013
The output is like this,

ls ---> list all files & directories from the current directory

grep -e test ---> filter all files/directories with the name having "test" in it

Code:
NOTE: this output will be line-by-line
Ex:
test
test.xml

awk -F " " '{print $1}' ---> This will push all the matched files to your variable "e"

I would suggest to use
Code:
e=$(ls | grep -e "test" | head -1)

This User Gave Thanks to PikK45 For This Post:
# 3  
Old 05-29-2013
How about using anchors?
Code:
e=$(ls | grep "^test$")

# 4  
Old 05-29-2013
Is the requirement for grepping the word "test" ALONE

or

to grep the first occurrence of the word "test" ??? Smilie
# 5  
Old 05-29-2013
Your requirements are extremely ambiguous. If I take them literally, I interpret it to mean that if I have files named attest, bestest, and test.c (or, in fact, one or more files with names containing the string "test" even if none of them are actually named "test"); you want to print test. And, if there are no filenames in the current directory that contain the string "test", you don't want to print anything. If that is what you want, the following will do that if you're using a recent bash or a Korn shell newer than 1988:
Code:
list=( *test* )
if [ ${#list[@]} -gt 1 ] || [ "${list[0]}" != "*test*" ]
then    echo test
fi

If that isn't what you want. Please clearly state what you are trying to do.
# 6  
Old 05-29-2013
Code:
$ ls test*
test  test~
$ ls | awk '/test/ {print $1; exit}'
test

This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-29-2013
Quote:
Originally Posted by PikK45
Is the requirement for grepping the word "test" ALONE

or

to grep the first occurrence of the word "test" ??? Smilie

Its the first occurence of the word test
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read multiple values into one variable

Hello everybody, I am trying to assign multiple values from text into a single variable in bash. Source TXT: 1 THIS IS THE ENTITY1 NAME1 2 THIS IS THE ENTITY2 NAME2 3 THIS IS THE ENTITY3 NAME3 while read id entity name do printf "$id" "$entity" "$name" done <... (2 Replies)
Discussion started by: mrcrowley
2 Replies

2. Shell Programming and Scripting

Interpreting multiple values from a variable

Hi, I am writing a shell script which will check the status of a resource in a cluster and then display nicely to a user running the script at command line. Basically the script runs a status command and then pulls certain keywords from the return and then should display a concise status. ... (5 Replies)
Discussion started by: chris01010
5 Replies

3. Shell Programming and Scripting

Passing multiple column values to UNIX variable

sqlplus -s $USER_ID@$SID/$PWD<<EOF>sql_1.txt set feedback off set heading off select 114032 as c_1 from dual ; EOF for i in `cat sql_1.txt` do sh script_1.sh $i Currently i am passing one column value to the single unix variable. How can i pass the values from 2... (2 Replies)
Discussion started by: rafa_fed2
2 Replies

4. Shell Programming and Scripting

Variable with multiple values

Hello I need to alter a script to check for a flag file but there are now more than one type of flag file in my directory. Basically if any of these flg files exist then the MASK value should be set? Flag files to be included in assignment to variable e2e_scheduled*.flg COLL_STOP*.flg... (1 Reply)
Discussion started by: andymay
1 Replies

5. Shell Programming and Scripting

Accepting multiple values in a variable at run time

Hi, Below is starting entry of my script #!/bin/ksh Usage() { print "Usage: $0 ID OPTION SERVER" print "<br>Where :" print "<br>Enter your ID into PARAM1, OPTION in the PARAM2 and SERVER in the PARAM3 field" print "<br>ID should be a valid ID" print "<br>OPTION should be either... (2 Replies)
Discussion started by: gopajitmalakar
2 Replies

6. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

7. Shell Programming and Scripting

help with assigning multiple values to a variable

I have a situation where my variable needs to pick up any of the 4 values from the environment it is in for e.g i am on server named a server=a (script running on this server) ftp servers= b c d e ----- the parameter passed should be any of these values in these 4 values, if not throw an... (4 Replies)
Discussion started by: dsravan
4 Replies

8. Shell Programming and Scripting

Using multiple values for single variable in a loop

Hello Guys, I have a small loop problem as below. I have 3 different values to be used while running the same script - va1="some-value1" va2="some-value2" va3="some-value3" Now I want to use these three variable values to be used for running the same command, like - while... (1 Reply)
Discussion started by: rockf1bull
1 Replies

9. Shell Programming and Scripting

Assigning multiple values to a variable

Hi Guru`s, I have to write a prog. which will traverse through diff. directories and then extract some data from files. I have written it and its working fine. But I have tested it in 1 folder. There are many folders and I need to loop through each of them. I am not sure abt the... (4 Replies)
Discussion started by: unx100
4 Replies

10. UNIX for Dummies Questions & Answers

Storing Multiple Values in a Variable

Hi, My code is as below: integer i=7 while ((i <= 5 )); do # test_out is a variable which contains data separated by "^". a= `echo $test_out | cut -d"^" -f$i` echo "$a" (( i = i + 1)); done From the above code, i kept $i after f so that i can capture all the data which is... (1 Reply)
Discussion started by: sandeep_1105
1 Replies
Login or Register to Ask a Question