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:

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 ?

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.

Hi Franklin,
i apologize for the same.

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

#!/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.

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

Try this:

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

instead of:

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

hi franklin,
thanks for your help :b:.

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 :confused: commented awk works as expected. how do i achieve the same result from the above output(ie $abc) ?

#!/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);
   # }
    #exit;
#}'
    fi

done
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.

1 Like

hi danmero,

That works with small modification ie print$2 is what i want.

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)

dir1/kernel
valid/out

input.xml contains

<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.

---------- 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:

<pro name="xyz/dir1/Test" path="valid/Test" revision="12345"/>

List:

valid/Test

Franklin code with field separator change it worked for all:

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

instead of

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

Base on sample data this should work.

# cat list
dir1/kernel
valid/out
# cat input.xml
<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"/>
# awk -F\" 'NR==FNR{a[$0]=$0;next}{if(a[$4]==$4)print $2}' list input.xml
abc/dir1/kernel
xyz/valid/out