find the line starting with a pattern and save a part in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the line starting with a pattern and save a part in variable
# 1  
Old 11-25-2010
find the line starting with a pattern and save a part in variable

Hi

i have a file which has mutiple line in it.

inside that i have a pattern similar to this

/abc/def/hij

i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable.


thanks in advance
# 2  
Old 11-25-2010
thru sed..
Code:
sed 's|^/\([^/]*\)/.*|\1|' inputfile > outfile

# 3  
Old 11-25-2010
or
Code:
echo "/test/test2/test4" | tr '/' ' ' | cut -f2 -d" "

# 4  
Old 11-25-2010
@research3: we could shorten it as
Code:
echo "/test/test2/test4" |  cut -f2 -d /

# 5  
Old 11-25-2010
that's true of corse!
# 6  
Old 11-25-2010
hello

thanks everyone

i have written code as

Code:
 
while read line; do
defaultStorage= `echo "/test/test2/test4/test6" | cut -f2 -d/`
echo $defaultStorage
done<defaultStore.txt


i am not getting the required result in defaultStorage
# 7  
Old 11-25-2010
there should be no space surrounding the = sign during variable assignment.So try as
Code:
defaultStorage=`echo "/test/test2/test4/test6" | cut -f2 -d/`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Save line from text in variable

Hi, I wrote a csh script where I want to save in a loop each time a different line from a text file (att_file) in the $name variable. But it seems not to work. att_file looks like: 123123123 345345345 345345345 set name = `head -n $count $att_file | tail -n 1 | awk '{print $1}'` Do... (3 Replies)
Discussion started by: MLImag
3 Replies

2. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

Save an specific part of a expect_out in a variable

I have a expect file like this #!/opt/tools/unsupported/expect-5.39/bin/expect spawn ssh -l user ip expect_after eof {exit 0} set timeout 10 log_file /report.txt expect "Password:" { send "pasword\r" } expect "$ " { send "date\r" } expect "$ " { send "readlink /somelink\r" } set... (7 Replies)
Discussion started by: bebehnaz
7 Replies

4. Shell Programming and Scripting

Remove duplicate line starting with a pattern

HI, I have the below input file /* ----------------- cmdsDlyStartFWJ -----------------*/ UNIX_JOB CMDS065J RUN ANY CMDNAME sleep 5 AGENT CMDSHP USER proddata RUN MON,TUE,WED,THU,FRI DELAYSUB 02:00 /* "Triggers daily file watcher jobs" */ ENVAR... (5 Replies)
Discussion started by: varun22486
5 Replies

5. UNIX for Dummies Questions & Answers

Removing Part of a variable based on a pattern

Hi All, I am writing a script to work with files in a folder. The files are all in the following patterns (without quotes): "some filename - NxNN - the end.YYY" or "some filename - NNxNN - the end.YYY" Where N = a single number and YYY is the extension. Basically what I want... (5 Replies)
Discussion started by: sgtbobie
5 Replies

6. Shell Programming and Scripting

how to find entries, NOT starting with specific pattern

Hey,I have a file in following format >1 ABC........ >2 XYZ..... >3 ABC........ >4 MNO....... >5 ABC....... now I would like to find only those entries that doesn't start with ABC (specific pattern)e.g preferred output: >2 XYZ.... >4 MNO....... it will be nice if anybody how... (2 Replies)
Discussion started by: ankitachaurasia
2 Replies

7. Shell Programming and Scripting

search a string in a line and save it in a variable

Hi I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable. Can someone suggest me the way to implement it.I am using K- shell Thanks Ishita (5 Replies)
Discussion started by: Ishita
5 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question