Small UNIX problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Small UNIX problem
# 1  
Old 03-18-2005
Small UNIX problem

I have a shell script that carries out an extraction process as follows:

For a given input file dummy which looks like the following:


a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3
b.txt
b1
b2
b3

the shell script returns two files called a.txt and b.txt followed by their corresponding content.
a.txt would contain the following content:
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

and b.txt would contain the following content:
b1
b2
b3


the problem is when i run the script where the first line of dummy is not x.txt i get errors.
Example if dummy contained

asdasda
fg
grtjiogoirjtgdjkljdfslkjdf
a.txt
qwe
ert

tyu



then when i run the script i get the following ouput:

./extracter: line 8: $FILENAME: ambiguous redirect
./extracter: line 8: $FILENAME: ambiguous redirect
./extracter: line 8: $FILENAME: ambiguous redirect

but it still produces a.txt


Here is the script:


cat dummy |while read LINE
do
if [ "`echo ${LINE} |grep '.txt'$`" != "" ] ; then
FILENAME=${LINE}
continue
fi
echo $LINE >> $FILENAME
done


does anyone know how i can fix the problem.

thanks
# 2  
Old 03-19-2005
sorry i haven't learned about shell programming but i learn a little about awk, so if you don't mind i will do it in awk. i modified earlier post in another thread so it give out the name of your file instead of number

gawk '/\.txt$/ { fn=$0;next} {print > fn}' < A

Code:
$ cat A
a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3
b.txt
b1
b2
b3

$ gawk '/\.txt$/ { fn=$0;next} {print > fn}' < A

$ ls
A  a.txt  b.txt

$ cat a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3

$ cat b.txt
b1
b2
b3

of course you could fix the regex to be more specific for your needs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Small problem

hello, i am a newbie and i really need your help i have an centos 6.5 and i wonder if i can create a list with random 4 letters : gdjf ornc tyrn jfdn nofd etc.... if it can be done , i will really apreciate your help ! THANKS !!! ---------- Post updated at 07:16 AM... (10 Replies)
Discussion started by: suppliernr1
10 Replies

2. IP Networking

Small Problem about Default Gateway

I have Ubuntu linux Os with two eth cards. I can easily delete def gatewayg. But I can't add new one.. :confused::confused: When I run route-n: alperen@alperen-System-Product-Name:~$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use... (1 Reply)
Discussion started by: Mhnds
1 Replies

3. Shell Programming and Scripting

SED help, small problem

Hi, I have this sed command to grep a date from a filename for a script we have. I am awful with sed so I need help. Sometimes it works fine but other times it does not, see below. works bash-3.00# echo... (1 Reply)
Discussion started by: GermanJulian
1 Replies

4. UNIX for Dummies Questions & Answers

A small AWK problem

I have a file tmp.out with contents: 2008-08-09 05:11:01 2008-08-09 08:52:59 2008-08-11 12:08:34 2008-08-11 12:15:40 I want the output to be: 3|0|1|71|2008-08-09 05:11:01|2008-08-30 11:19:28 4|0|1|71|2008-08-09 08:52:59|2008-08-30 11:19:28 5|0|1|71|2008-08-11 12:08:34|2008-08-30 11:19:28... (6 Replies)
Discussion started by: ChicagoBlues
6 Replies

5. UNIX for Dummies Questions & Answers

small problem

Hey everyone! What's the command to check the last time a file was "touched"? Thank you. (2 Replies)
Discussion started by: yosef_yaniv
2 Replies

6. SCO

Small Problem with SCO

Help Please , how to shutdown SCO v. 5.0.5 from client pc ( windows xp or 2000 ) ? i have full access from client and i need to shutdown SCO from windows (2 Replies)
Discussion started by: rabih_saleh
2 Replies

7. IP Networking

Small problem...

Nice pictures, really free from virtual swingers club !!!! Anal sex Body art Gay Hardcore Lesbian Oral sex Cum XXX Models Private Image Galleries Private XXX photo Free!!! Say no to Spamers!!! Auto forum spam msg replacer by Swinguru © (1 Reply)
Discussion started by: Green_Monkey23
1 Replies

8. Shell Programming and Scripting

small date problem

Hi there I currently use a line in a script ndate=$(date +"%d/%m/%Y") This obviously returns the date in a format i have chosen. However, I need to come up with a solution for weekends and as such need a way of returning todays date minus two days in the same format (24/11/2004) so for... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

9. Programming

a small problem

hey there i kinda have a small/big problem. i have created a 'small' version of a shell that only accepts ls comamnds. my problem is that when i compiled the program and got it to work, i deleted the .c file from which the shell was created. but now, i would like to do some more work on the... (2 Replies)
Discussion started by: mile1982
2 Replies

10. UNIX for Dummies Questions & Answers

small unix!

i'm looking to put a very minimal unix on a 386 i just acquired. it needs to be downloadable and i need to install it using 3.5 floppies! thanks for suggestions, -nydel (4 Replies)
Discussion started by: nydel
4 Replies
Login or Register to Ask a Question