LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2009, 02:18 PM   #1
wrankin
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Rep: Reputation: 0
Question Problem using sed with variable created using xargs - unterminated error


Hey all. I have an awk program that finds all files of a specific filename and deletes them from selected subdirectories. There is logic in the awk to avoid certain subdirectories, and this is initialized via a parameter in the beginning statement of the awk. The parameter should have all of the subdirectory names at the top level. This varies from time to time, so I cannot hard-code the value.

I'm having a problem initializing the awk parameter using sed. I'm setting a variable (named subdir) using an "ls" command piped to "xargs". I'm then trying to substitute that value into the awk using the sed command. Here is what I have:

awk program
BEGIN {
dirlist = "XYZ";
dirnum = split(dirlist,dirs);
}
{ Process stuff based on value in dirs... }

main bash script

subdir=$(ls /filestructure/containing/four/subdirectories/ | xargs -0)

echo $subdir
Result: 000 123 ABC LOG

sed 's/XYZ/$subdir/' first.awk > updated.awk
Result: awk has line dirlist = "$subdir";
instead of dirlist = "000 123 ABC LOG";

sed "s/XYZ/$subdir/" first.awk > updated.awk
Result: sed: -e expression #1, char 9; unterminated `s'

Any help would be very much appreciated...thanks in advance.
 
Old 01-12-2009, 02:24 PM   #2
wrankin
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Original Poster
Rep: Reputation: 0
I've also tried putting the variable outside of the single quotes within double quotes like below, but it still fails.

sed 's/XYZ/'"${subdir}"'/' first.awk > updated.awk
Result: sed: -e expression #1, char 9; unterminated `s'
 
Old 01-13-2009, 06:18 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi, wrankin.

Awk allows to initialise variables from command line. In your case you may use something like this:
Code:
awk -v dirlist="$subdir" -f first.awk
 
Old 01-14-2009, 03:54 PM   #4
wrankin
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks firstfire, that's a great thing to know.

My impression is that my problem has more to do with the value of the variable "subdir", because it contains end of line breaks. I got around it by doing the command "echo" which eliminated the breaks into a sed command file, and then using it.

echo 's/XYZ/'$subdir'/' > sedcmd.txt
sed -f sedcmd.txt first.awk > updated.awk

I'll probably start using firstfire's suggestion of initializing variables in awk from the command line in the future.

Thanks again firstfire.
 
Old 11-14-2011, 12:10 PM   #5
hunter3740
LQ Newbie
 
Registered: Mar 2010
Location: Pittsburgh
Distribution: Debian 6
Posts: 28

Rep: Reputation: 17
related, but slightly different

I too was getting an xargs: unterminated error when running my command, which wasn't too hard to zero in on, as my error was "xargs: unterminated quote". Turned out to be a single quote in many of my file names that wasn't parsed right when double-quoting the path/filenames (so as to try to avoid having to escape all the special characters):

# note: file.txt is a cleaned up output of an rsync -avn --delete command, that's just a list of path/filenames
sed "s/\'/\\\'/g" file.txt >newfile.txt
cat newfile.txt | xargs -I % mv "/mountfolder/%" "/anothermount/%"

Last edited by hunter3740; 11-14-2011 at 12:14 PM.
 
Old 11-14-2011, 08:53 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,965

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
So far I see bash, awk, sed, xargs and even ls ... why?

I might not be understanding the actual question here because this is one of those questions where someone says ... I am doing something 'this' way and
want to fix blah to get it to work .... When really you should be telling us what it is you want to do and then what you have tried. The reason this is different
is it allows helpers to show you that your current method may in fact be the issue as opposed to a single part of it.

I believe this is the case here. From the current explanation, it appears you wish the following:

1. Find all files with a specific name and delete them
2. Exception to rule 1 is that you have a certain path you do not wish to follow

Now you may be doing more processing than this but of course you do not mention it, so the above is a simple find, something along the lines of:
Code:
find /path/to/start -path /path/to/be/excluded -prune -o -type f -name 'specific_file_name_here' -exec echo rm {} \;
If this gives you the output you want then simply remove the echo and rm will remove the chosen files.

If I have completely misunderstood the requirement here please let me know?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM
sed help with variable RudraB Programming 1 07-04-2008 02:10 AM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 11:54 PM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 12:22 PM
sed and variable chirophil91 Programming 2 10-19-2004 04:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration