Advanced grep'in... grep for data next to static element.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Advanced grep'in... grep for data next to static element.
# 1  
Old 01-05-2011
Advanced grep'in... grep for data next to static element.

I have a directory I need to grep which consists of numbered sub directories. The sub directory names change daily. A file resides in this main directory that shows which sub directories are FULL backups or INCREMENTAL backups.

My goal is to grep the directory for the word "full" and then preceding that word in the file is the name of the sub directory the FULL backup is in. So I then need to grab that name and execute a tar or cp command on that directory.

EXAMPLE: contents of the directory are: 123 124 125 126 file.txt file2.txt

In file.txt there is a line that reads: [backup 123 full type etc etc]
This tells me the FULL backup is in the 123 directory (the data right in front of the word FULL)

I then need to tar or cp the 123 directory.
# 2  
Old 01-05-2011
Code:
directory=$(awk ' /^[backup/ {print $2}'  file.txt)

try that.
# 3  
Old 01-05-2011
Thanks for replying, but did not work - regular expression compile failed (bad class -- [],[^] or [)

Looks like the [ in front of the work backup... but...

Here is the exact data in the file that I need to grep:

123 full 129234532 123234122 1297 2123121 1231211 123121 rsync 0 0
124 incr 129232342 129452323 1232 2342322 1123232 112312 rsync 0 0

I need to know the data in front of the word FULL... that is the directory name that I need to cp or tar...

Thanks for replying...

---------- Post updated at 01:59 PM ---------- Previous update was at 11:22 AM ----------

After some research I believe I got it!!

awk '/full/ { print $1 } ' myfile

this finds the word full, then prints out the first column in the file, which happens to be the directory name in my case....

Now to pipe this output to a tar command to look for that directory name... done.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

2. Shell Programming and Scripting

Grep exif data

I would like to search for regex in the exif data of a large collection of files (mostly jpg, some nef). For instance "grep_exif Tokyo *.jpg" should return the file names of all files which have "Tokyo" in their exif data. Exiftool could be used to extract the data. I don't really need to grep... (6 Replies)
Discussion started by: bamse
6 Replies

3. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

4. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

5. Shell Programming and Scripting

How can i grep for an hour before data

Hi, My log file is something like this. (08/04/2009 00:27:42.179)(:) aaaaaaaaaaaa (08/04/2009 00:27:42.181)(:) bbbbbbbbbbbbbbbb (08/04/2009 01:00:42.713)(:) cd cdc d ddddsksjdkssksksj (08/04/2009 01:02:42.716)(:) raarrarararararara (08/04/2009 01:07:43.036)(:ERROR) Port... (8 Replies)
Discussion started by: rdhanek
8 Replies

6. Shell Programming and Scripting

Advanced grep and sed

I am wondering if there is a way via grep and sed to extract a string that is on the 2nd line below a known marker as in this example: TextRel 203 0 0 "WELL:" SetPosAbs 1287 -6676 TextRel 210 0 0 "AEP #2" The marker is WELL:, but the string I need is "AEP #2". Can grep/sed handle this... (19 Replies)
Discussion started by: phudgens
19 Replies

7. Shell Programming and Scripting

i want to grep some data from other file

helo all i have 2 files. and i want to grep the contents of first file from the 2nd file. but these files are too large contaning lacks of lines . i'm using for loop but it takes so moch times . is there any other sol. i'm using this code " for var in `cat succ_migrated` do grep $var... (4 Replies)
Discussion started by: dodasajan
4 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

How to use grep to check NAN value and nonexistense element in file

Hi, I have a file which computes fields number and some column produced "nan" I would like to convert the "nan" value to 0 basically. q=`echo $i $j |awk '{printf("%f",($2/($1+$2)))}' The above is the command which I use for computation. However, I would like to check if $1 and $2 is 0, it... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

grep data from files

Hello Pls help me i need script for counting number of fails from file . The file contains number of failuer records . (2 Replies)
Discussion started by: getdpg
2 Replies
Login or Register to Ask a Question