search a word and print specific string using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a word and print specific string using awk
# 1  
Old 09-02-2010
search a word and print specific string using awk

Hi,
I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire line how do i filter the path using awk special variables?

if $i has dir1/kernel if it exists delete otherwise grep "dir1/kernel" from input.xml file read the project name path and delete the folder"abc/dir1/kernel" and continue with loop.No space between name,=," so how do i use FS & what should be the search pattern in awk?

out.txt contains
<pro name="abc/dir1/kernel" path="dir1/kernel" revision="12345"/>
<pro name="xyz/valid/out" path="valid/out" revision="12345"/>
code:

Code:
for i in ${REMOVE_DIRS}
do    
    if [ -d "${i}.git" ]; then
    echo "Remove ${i}.git"
    rm -rf ${i}.git
    else
    echo "directory ${i}.git does not exist"
    grep "${i}" input.xml >> out.txt
    fi

done

I appreciate your ideas .

---------- Post updated at 06:19 AM ---------- Previous update was at 03:42 AM ----------

any thoughts ?
# 2  
Old 09-02-2010
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 3  
Old 09-02-2010
MySQL search a word and print specific string using awk

Hi Franklin,
i apologize for the same.

i tried little more with awk and came almost closer (i guess)

Code:
#!/bin/bash
awk 'BEGIN {
string="name=\"abc/dir1/kernel\"";
    search="\"";
    n=split(string,array,search);
 for (x=1;x<=n;x++) {
    printf("Word[%d]=%s",x,array[x]);
#  printf("%s",array[2]);
    }
    exit;
}'

Here i get the expected output: Word[1]=name=Word[2]=abc/dir1/kernel
word[2] is what i wanted .

From below mentioned code,Problem i have is $abc variable has name="abc/dir1/kernel" so i couldnt split it similar to above mentioned code.I appreciate all your ideas / help.

Code:
for i in ${REMOVE_DIRS}
do    
    if [ -d "${i}.git" ]; then
    echo "Remove ${i}.git"
    rm -rf ${i}.git
    else
    echo "directory ${i}.git does not exist"
    grep "${i}" input.xml >> out.txt
    abc=`awk ' "${i}" { print $2 } ' out.txt`
    echo $abc
    fi

done


Last edited by Scott; 09-02-2010 at 03:27 PM.. Reason: Code tags, please...
# 4  
Old 09-02-2010
Try this:
Code:
abc=`awk -F= -v abc="${i}" '$0 ~ abc { print $2 }' input.xml`

instead of:
Code:
grep "${i}" input.xml >> out.txt
abc=`awk ' "${i}" { print $2 } ' out.txt`

# 5  
Old 09-02-2010
MySQL search a word and print specific string using awk

hi franklin,
thanks for your help Smilie.

input.xml contains (example)
<pro name="abc/dir1/kernel" path="dir1/kernel" revision="12345"/>
<pro name="xyz/valid/out" path="valid/out" revision="12345"/>

Output didnt help much to solve the split ie print$2 gives
"abc/dir1/kernel" path
"xyz/valid/out" path

From my code output was
name="abc/dir1/kernel"

print$1 gives
<project name

print $3 gives
"dir1/kernel" revision
"valid/out" revision

Because of double quotes i am unable to use string in split Smilie commented awk works as expected. how do i achieve the same result from the above output(ie $abc) ?

Code:
#!/bin/bash
REMOVE_DIRS=`cat List`
# Remove dirs
#if [ -f try.log ]; then
#rm -rf try.log
#fi
for i in ${REMOVE_DIRS}
do    
    if [ -d "${i}.git" ]; then
    echo "Remove ${i}.git"
    rm -rf ${i}.git
    else
    echo "directory ${i}.git does not exist"
  #  grep "${i}" input.xml >> out.txt
 #   abc=`awk ' "${i}" { print $2 } ' out.txt`
    abc=`awk -F= -v abc="${i}" '$0 ~ abc { print $1 }' input.xml`
    echo $abc
    # split and  remove the dir
#awk 'BEGIN { str="\"abc/dir1/kernel\" path";search="\"";
   # n=split(str,array,search);
 #for (i=1;i<=n;i++) {
   # printf("dirpath[%d]=%s",i,array[i]);
   # }
    #exit;
#}'
    fi

done


Last edited by dragon.1431; 09-02-2010 at 05:59 PM.. Reason: keeping the correct name in str
# 6  
Old 09-02-2010
Code:
awk -F\" -v dir="dir1/kernel" '$2~dir{print $4}' file

If is not what you want , please post a sample of your List file, xml file and desired output base on example.
This User Gave Thanks to danmero For This Post:
# 7  
Old 09-03-2010
hi danmero,

That works with small modification ie print$2 is what i want.
Code:
abc=`awk -F\" -v dir="${i}" '$2~dir{print $2}' input.xml`

can you please explain a little bit about the code?

List filecontains (example)
Code:
dir1/kernel
valid/out

input.xml contains
Code:
<pro name="abc/dir1/kernel" path="dir1/kernel" revision="12345"/>
<pro name="xyz/valid/out" path="valid/out" revision="12345"/>
<pro name="xyz/dir1/Test" path="valid/Test" revision="12345"/>

First check for presence of dir1/kernel if exists remove it else search the string dir1/kernel in input.xml file and get the output as abc/dir1/kernel from <pro name="abc/dir1/kernel" . once i have new or correct path delete dir abc/dir1/kernel.

Moderator's Comments:
Mod Comment Please start using code tags more frequently and also for snippets of logs, data etc. to separate them from descriptive text, for better readability and all the rest of it, ty.


---------- Post updated at 02:30 AM ---------- Previous update was at 01:48 AM ----------

Hi danmero,franklin,

Thanks for your help.

it didnt work for the following data:
Code:
<pro name="xyz/dir1/Test" path="valid/Test" revision="12345"/>

List:
Code:
valid/Test

Franklin code with field separator change it worked for all:
Code:
abc=`awk -F\" -v abc="${i}" '$0 ~ abc { print $2 }' input.xml`

instead of
Code:
abc=`awk -F= -v abc="${i}" '$0 ~ abc { print $2 }' input.xml`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

4. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

5. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

8. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

9. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question