awk inside shell and script aborting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk inside shell and script aborting
# 1  
Old 03-31-2014
awk inside shell and script aborting

Hi
I have a requirement where i need to convert the xml in various lines into a single line and then split the file so that the file contains only 1000 records each.

searching this forum, i found a command which converts the xml in various line
into a single line, but i have trouble using that in the shell script, as the script
reads the parameters variables as commands..

below is the command i found

Code:
awk '/<cmk:Service/{ORS="";if(NR!=1)print "\n";}1' /data/file1.xml > /data/file2.txt

below is the script i prepared, its aborting with
Code:
Service: not found, no such file or directory

Code:
$1 = /<cmk:Service/
$2 = /data
$3 = file1.xml
$4 = file2.txt
$ = file ( prefix for split command)

Code:
#!/bin/sh
awk '$1{ORS="";if(NR!=1)print "\n";}1' $2/$3 > $2/$4
split -l 1000 $2/$3 $5

for i in $( ls $5* )
timestamp=`date "+%Y%m%d%H%M%S"`
do
mv -f $5* $5_$timestamp_$i.dat
done

can someone help me on this?

thanks
MJ

Last edited by Don Cragun; 03-31-2014 at 11:09 PM.. Reason: Add CODE tags.
# 2  
Old 03-31-2014
$1 is not being expanded as it's within single quotes

try this:

Code:
awk "$1"'{ORS="";if(NR!=1)print "\n";}1' $2/$3 > $2/$4

or with $1="<cmk:Service"

Code:
awk -v W="$1" '$0 ~ W{ORS="";if(NR!=1)print "\n";}1' $2/$3 > $2/$4

# 3  
Old 04-01-2014
Hi
thanks for your response..

yes, the first step is working with your code..

for second step of splitting the file and renaming, i have modified the code like below

what i'm trying to do is, after i created the single line xml file, i'm splitting that file each
containing 1000 records, by assigning the prefix($5), (this is working fine)

after i splitted the files, i need to rename all the splitted file with $5 and concatinating timestamp and and a individual sequencer number, like below
file2_201401312231_1
this is the part which is not working, can you please help

Code:
#!/bin/sh
awk -v W="$1" '$0 ~ W{ORS="";if(NR!=1)print "\n";}1' $2/$3 > $2/$4

split -l 1000 $2/$3 $5

num=1
for i in $( ls $5* )
timestamp=$(date "+%Y%m%d%H%M%S")
do
num=num+1
mv -f $i $5_$timestamp_$num
done

# 4  
Old 04-01-2014
try this:
Code:
mv -f "$i" "${5}_${timestamp}_${num}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

2. Shell Programming and Scripting

How to merge Expect script inside shell script?

Hi I have two scripts one is Expect and other is shell. I want to merge Expect code in to Shell script so that i can run it using only one script. Can somebody help me out ? Order to execute: Run Expect_install.sh first and then when installation completes run runTests.sh shell script. ... (1 Reply)
Discussion started by: ashish_neekhra
1 Replies

3. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Search the shell variable inside awk

Hai, I need to search a variable inside a file using awk in AIX. for ex: file.txt one two three four five i need to get the lines with two awk '/two/ {print}' file.txt But i need to change the two as a variable since it would be changed on runtime..i tried like below..nothing give... (3 Replies)
Discussion started by: jesu
3 Replies

6. Shell Programming and Scripting

how to run shell script inside expect script?

I have the code like this : shell script continues ... .... expect -c" spawn telnet $ip expect "login:" send \"$usrname\r\" expect "Password:" send \"$passwd\r\" expect "*\>" send \"$cmdstr\r\" ... (1 Reply)
Discussion started by: robbiezr
1 Replies

7. Shell Programming and Scripting

Call a perl script inside a shell script

Hi all, I have the following snippet of code.. #!/bin/sh echo "run perl script............" #Run the verification script perl bill_ver echo " perl script completed....." echo "rename files......" #Remove from all file in the directories test, test1, test2, test3 for f in... (3 Replies)
Discussion started by: chriss_58
3 Replies

8. Shell Programming and Scripting

invoking a shell script inside cgi shell script

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

9. UNIX for Advanced & Expert Users

Script is aborting if there is no file

HI! Iam running two scripts in my unix server First script is Splitting a file containing 300 records each with .tmp extension for eg Z_PRICE_NEW.tmp Second script is converting .tmp to.txt for file in Z_PRICE_NEW_2*.tmp ; do mv $file `echo $file |sed 's/\(.*\.\)tmp/\1txt/'`;done ... (1 Reply)
Discussion started by: mohdtausifsh
1 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question