sed / grep question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed / grep question
# 1  
Old 02-23-2006
Tools sed / grep question

Hello,

I am new to shell scripting.

I have a input file: Few lines of the input file has the following.

/a0012/abcd12/abcd12
/a0003/xyzab1/lmno123
/a0006/pqrst1/abcde12

In my output file, I only need to get anything that is between the first and second '/' of each line: exampple:
a0012 a0003 a0006

Using grep I have got the above three lines from a huge input file. But not sure how to go forward with my problem.

Please help.

thanks
Hemang
# 2  
Old 02-23-2006
use the following

awk -F '/' '{print $2}' f1.txt

where your data is in f1.txt

Gaurav
# 3  
Old 02-23-2006
Gaurav,

Thanks..

But,
(1) The output is on different lines...
I want them all to show up on one line with 'space' as a seperator.

(2) I would appreciate if you could you also explain the syntax to me. I am kind of learning on own while working on it.

thanks again.
# 4  
Old 02-23-2006
one more question

One more question:

How can i assign the output value to a variable?

I tried the below and did not work.
Not sure what syntax is required.
awk_var=awk -F '/' '{print $2}' file.input

Please help.
thanks
# 5  
Old 02-23-2006
Instead of print use printf("%s ",$2).

What type of variable: shell's or awk's ?
# 6  
Old 02-24-2006
Quote:
Originally Posted by hemangjani
One more question:

How can i assign the output value to a variable?

I tried the below and did not work.
Not sure what syntax is required.
awk_var=awk -F '/' '{print $2}' file.input

Please help.
thanks
Hi,
You had three quesions,

1) fidodido already replied for it, thanks fidodido for telling such a simple way of doing it

2)Go through man pages for awk you will understand it and may be go through sed and awk book from orelly or some other resources for awk using google

3)use the following

awk_var=`awk -F '/' '{print $2}' file.input`

if still some confusion post back to here

Gaurav
# 7  
Old 02-24-2006
Thank you Fidodido and Gaurav.
It worked both ways.

Fidodido,
The variable I was trying to use was a shell variable.
It is working now. I had syntax issues.

thanks again..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep / awk /sed question

Hi All, I have a file with following sample data. I am also attaching the file. (Est) (Est) Jobs Sch Workstation Job Stream SchedTime State Pr Start Elapse # OK Lim POOL #ACR_BILLING 0005 08/15 ... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

2. Shell Programming and Scripting

Question grep and sed

hi everybody i have this script and it work's if i use a single variable in grep, but when i put $searchterm_ the script stops work i have a problem too in sed, because i don't know how i can search and replace more than one item, in this case is >>> $searchterm/$replaceterm and... (4 Replies)
Discussion started by: felito
4 Replies

3. Shell Programming and Scripting

grep question

Hello, Is there a way in grep to remember patterns? For eg: int a,b,c,d,a; If a variable is declared twice, like in the previous example, I should be able to print only those lines. Is there a way to print only the lines where the variable name occurs more than once, using grep... (1 Reply)
Discussion started by: prasanna1157
1 Replies

4. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

5. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

6. UNIX for Dummies Questions & Answers

grep question

how can you create a script that would show the words you grep for as so grep -i WORD file.name This is where the WORD is. ^^^^ the ^ should be the same number of letters and directly or nearly under the word in question. thanks (1 Reply)
Discussion started by: farmerjack86
1 Replies

7. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

8. Shell Programming and Scripting

Question on Grep!

Hi all, I am relatively new to Unix... Pls help me with the following : is there an option ion grep to find the first and last occurence of a pattern in a file? if not how else can this be accomplished? Cheers, Bourne. (5 Replies)
Discussion started by: bourne
5 Replies

9. Shell Programming and Scripting

question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable. such as: a="hello world" b="how are you" c="best wish to you" d="222,333,444" what if i want to check which variable $a,$b,$c,$d have contain "you" what if i want to replace the word "you"... (9 Replies)
Discussion started by: 3Gmobile
9 Replies

10. Shell Programming and Scripting

grep & sed question

I'm trying to write a bash script to perform a tedious task, but I have no experience and hardly any knowledge so I've been having a rough time with it. I'm on Mac OS X, and I want a script to do the following: I have a directory that has about 200 sudirectories. In each of these directories,... (1 Reply)
Discussion started by: der Kopf
1 Replies
Login or Register to Ask a Question