Isolating a specified line - awk grep or somthing else?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Isolating a specified line - awk grep or somthing else?
# 1  
Old 06-09-2009
Isolating a specified line - awk grep or somthing else?

I'm trying to isolate attached hard drives that auto-mount to /media so that I can use them as variables in a bash script...

so far I'm here:

Code:
variable=$(ls /media | grep -v cdrom )

This lists all the connected drives, each on it's own line and doesn't list anything I don't want (cdrom folders)... is there a way using grep or awk to take the output of my "grep -v cdrom" and print a specific line...

I'll only ever have 6 drives connected at time, so I figure I'd just use the code 6 times to get each drive and keep it simple... Obviously the below script doesn't work but, something like this...

Code:
drive1=$(ls /media | grep -v cdrom | awk line1 )
drive2=$(ls /media | grep -v cdrom | awk line2 )
drive3=$(ls /media | grep -v cdrom | awk line3 )
drive4=$(ls /media | grep -v cdrom | awk line4 )
drive5=$(ls /media | grep -v cdrom | awk line5 )
drive6=$(ls /media | grep -v cdrom | awk line6 )

I did quite a bit of searching for this, but I must not using the correct terminology, because I couldn't find anything?

Thanks for any help!
-Starcast

Last edited by Starcast; 06-09-2009 at 04:47 PM..
# 2  
Old 06-09-2009
Code:
array=( ls /media | grep -v cdrom | tr -s '\n' ' '  )
i=0;
while [[ $i -lt ${array[#]} ]] 
  echo "array element # $1 = ${array[i]}
  i=$(( $i + 1))
done

# 3  
Old 06-09-2009
Quote:
Originally Posted by jim mcnamara
Code:
array=( ls /media | grep -v cdrom | tr -s '\n' ' '  )


I think you meant:

Code:
array=( $(ls /media | grep -v cdrom | tr -s '\n' ' ')  )

But that is not portable. Most shells do not have arrays. (And there's no need for tr in those shells that do have arrays.)
Quote:
Code:
i=0;
while [[ $i -lt ${array[#]} ]] 
  echo "array element # $1 = ${array[i]}
  i=$(( $i + 1))
done


There's no need for any external commands:

Code:
n=1
for d in /media/*
do
  case $d in
    *cdrom*) ;;
    *) eval "drive$n=\$d"; n=$(( $n + 1 )) ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

3. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

4. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

5. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

6. UNIX for Dummies Questions & Answers

Grep /Awk letters X - X in every line and print it as a mac address

hey i m kinda new to this so i will appreciate any help , i have this list of values: pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200 pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300 pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id =... (4 Replies)
Discussion started by: boaz733
4 Replies

7. Shell Programming and Scripting

Problems with Sed/awk/grep and line endings

Hello I have created the following script, which is designed to manipulate a text document: #!/bin/sh # Get 3 lines, (last of which is "Quantity"); adjust order; put all three on one line with tabs. FILENAME=~/Desktop/email.txt LIST=$(grep -B2 "Quantity" ${FILENAME} |awk 'BEGIN { FS = "\n"; RS... (6 Replies)
Discussion started by: benwiggy
6 Replies

8. Shell Programming and Scripting

awk, grep 'df -lah' into one line

Hi Everyone, # df -lah Filesystem Size Used Avail Use% Mounted on /dev/sda7 75G 59G 13G 83% / /proc 0 0 0 - /proc sysfs 0 0 0 - /sys devpts 0 0 0 - /dev/pts /dev/sda5 ... (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

df -k | grep (somthing)

Hi all, I am getting an hole out put of df -k for df -k | grep / Please let me how can i get for exact one... i want to grep it..! More ever.. Want to know few things...! I am using solaris 9 and want know how to get the CPU utlization from TOP command want calulate CPU utlization... (3 Replies)
Discussion started by: bullz26
3 Replies

10. SuSE

kazaa or somthing like it?!?!?!?

hey i'm looking for somthing like kazaa that i can use to download some music on, i've relly liked teh progs you guys recommended for MSN so i figured i'd get soem more recomendations....lol thanks. (7 Replies)
Discussion started by: byblyk
7 Replies
Login or Register to Ask a Question