Help with sed/awk

This is what i am trying to do. I have a file which has the following

file name-new.txt

*
include "/net/proj/new/1.txt"
include "/net/proj1/new/2.txt"
*

What I would like is to copy the file which is in the include statement

  1. Read new.txt
  2. copy 1.txt from /net/proj/new and 2.txt from /net/proj1/new/ which is in the include statement.

I have tried by zipping the file but it zips the entire path which doesnt work for me.

thanks in advance.

I'm confused, some words must be missing! Copy where for what? Do you want to preprocess it to include the include files in a temp file with no includes, like cpp or cc -E?

What I meant was while parsing the file, it should find all the places where there is an include statement and whatever is there in the include statement, I should be able to copy that file somewhere, may be temp. The include statement always has a path to a file.

I hope I have explained it better.

Do you need to parse include files for further includes (i.e should the script also check 1.txt and copy any files that it includes)?

new.txt
  |
  |---> 1.txt
  |       |
  |       ---> 1A.txt
  |
  L---> 2.txt

---------- Post updated at 11:44 AM ---------- Previous update was at 11:36 AM ----------

If not this should do the trick (copies to a temp sub directory under where new.txt is).

awk '
  /^[ \t]*include "/ {
     file=$0
     sub("[^\"]*\"", "",file)
     sub("\"$","",file)
     system("cp " file " ./temp")}' new.txt

Chubler,

Thanks, let me try it.

Now that you mentioned, 1.txt might have includes too.

Hi

Try this:

grep include new.txt | sed 's/include *\(.*\)/cp \1 \/temp/' | sh

Guru

 
cp $(sed -n 's/^include //p' new.txt) /temp

@Chubler_XL: I tried your script and it gives me "unmatched" something is not matching. Doesnt give me a lot to debug.

@guru: tried your sed command and it comes up with message ".cp : command not found"

@DGPickett: tried your sed command and it comes up with message "Illegal variable name"

try:

while read line1 line2
do
file=`sed 's/"//g' <(echo $line2)`
cp  $file /tmp
done <new.txt

@yinyuemi

I tried your code and when I execute it

=/usr/proj/1.txt : cannot open (=/usr/proj/1.txt)
cp: missing destination file
Try cp--help

This is what I have in new.txt

.include '/usr/proj/1.txt'

It seems that the single quotes in your "new.txt" file are causing trouble.

while read line1 line2
do
  file=`sed 's/["'\'']//g' <(echo $line2)`
  cp  "$file" /tmp
done < new.txt

try this?

 while read line1 line2
do
file=`sed 's/'\''//g' <(echo $line2)`
cp $file tmp/
done

Sounds like you might be using the csh shell (or another weird shell that dosn't support single quote over more than 1 line).

Try putting this updated awk program (added support for single quotes and optional period in front of include command) in a file called cpinclude:

/^[ \t\.]*include ["']/ {
     file=$0
     sub("[^\"']*[\"']", "",file)
     sub("[\"']$","",file)
     system("cp " file " ./temp")
}

And then from your csh (or whatever) prompt, where % is your prompt do:

% awk -f /path/to/cpinclude new.txt

@scottn & @yin

Both the codes seems to work but in the statement

file=`sed xxxx`

It executes the sed statement correctly but puts an '=' sign before so when the cp command is executed it fails because it has an '=' sign also
It says "cannot open (=)

---------- Post updated at 05:58 PM ---------- Previous update was at 05:57 PM ----------

@chubler

I am using csh shell

I did what you said and I get

' in expression1: ^ invalid char'

Try nawk instead of awk

@chubler: I dont have the nawk executable, I see gawk. I do not have the rights to install the rpm too.

Well, I'm at a loss, I don't believe anything in the awk script is non POSIX. Perhaps your awk is fairly old. What OS are you on? Is gawk throwing the same error?

The version of linux I have is

Red Hat Enterprise Linux WS release 4 (Nahant Update 5). Gawk also throws the same error.

I suspect you created the file on windows (eg with notepad) and then copied it to linux. Try dos2unix cpinclude on your linux platform, this should sort things out (windows puts extra \r on the end of each line and this isn't compatable with linux.

This could also be why the scripts offered by others here haven't worked out for you.

@chubler: You are right, dos2unix worked and the script does work now. I did create on the windows side for this one. Not for the other ones though. They still seem to have the problem. Thanks Bud.

One thing I found in my conditions are that the include statements can have statements as below which seems to crap out.

.include '/net/proj/1.txt' section

It says "unexpected EOF while looking for matching ' "

Also, I might have include statements within the file that is copied from the first include statement. May be I am asking for too much :wink: