ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a very simple package management script for use with CLFS which I won't explain the details of it but I need to make some modifications to the output of "du" and then flip the output upside down. I have made my modifications, I just need to flip the output of du upside down (so the first line would be last and the last line would be first).
I decided to use an existing gnu script and insert it as a function named "reverse". When I call this function with the command "reverse <FILENAME>" it outputs the contents of the file flipped. This is what I want it to do. But if I use the command "cat <FILENAME> | reverse" it outputs "bash - file-name". How would I modify this script so that it can use a pipe correctly?
Any help would be appreciated. Also, feel free to explain how the below function works if you feel like it.
Code:
function reverse
{
FILE="$1"
if [ $# -eq 0 ]; then
echo "$(basename $0) - file-name"
exit 1
fi
textArray[0]="" # hold text
c=0 # counter
# read whole file in loop
while read line
do
textArray[c]=$line # store line
c=$(expr $c + 1) # increase counter by 1
done < $FILE
# get length of array
len=$(expr $c - 1 )
# use for loop to reverse the array
for (( i=$len; i>=0; i-- ));
do
echo ${textArray[$i]}
done
}
As for your script, you are explicity testing for an argument (which will be absent when reading from stdin):
Code:
if [ $# -eq 0 ]; then
echo "$(basename $0) - file-name"
exit 1
fi
I don't use bash, so I'm not sure how best to differentiate between the filename and stdin. Zsh can reverse the file with a one-liner,
Code:
print -l ${(fOa)"$(<file)"}
Thanks for the reply but rev doesn't do what I want. I only want the order of the lines reversed, not the lines themselves. For example, if I have a file with these contents:
Quote:
Line 1
Line 2
Line 3
Then after I apply the reverse function I get
Quote:
Line 3
Line 2
Line 1
Which is what I want. The rev command would give me:
#!/bin/zsh
if [[ -n $1 ]]; then
print -l ${(fOa)"$(<$1)"}
else
array=()
while read line; do
array+=$line
done
print -l ${(Oa)array}
fi
If you must use bash, you might need to duplicate the while loop and wrap it in an if test:
e.g.
Code:
if [[ -n $1 ]]; then
while read line
do
textArray[c]="$line" # store line
let "c+=1"
done < $1
else
while read line
do
textArray[c]="$line" # store line
let "c+=1"
done
fi
Note that I replaced 'c=$(expr $c - 1 )' with the simpler 'let "c+=1"'. And, of course, remove the if test on $# at the start.
Last edited by soggycornflake; 11-05-2006 at 03:58 PM.
#!/usr/bin/sed -nf
# reverse all lines of input, i.e. first line became last, ...
# from the second line, the buffer (which contains all previous lines)
# is *appended* to current line, so, the order will be reversed
1! G
# on the last line we're done -- print everything
$ p
# store everything on the buffer again
h
Yes, of course! I momentarily toyed with mentioning sed but couldn't remember how to do it offhand (and was too lazy to RTFM). That's nice, keefaz, much better than arsing around with a shell array.
#!/usr/bin/sed -nf
# reverse all lines of input, i.e. first line became last, ...
# from the second line, the buffer (which contains all previous lines)
# is *appended* to current line, so, the order will be reversed
1! G
# on the last line we're done -- print everything
$ p
# store everything on the buffer again
h
I saved the above as a executable file named "sed_reverse" under ~/bin and called it like so:
Quote:
cat du1.txt | sed_reverse
And I got this error:
Quote:
bash: /home/ubuntu/bin/sed_reverse: /usr/bin/sed: bad interpreter: No such file or directory
I think it would be easier just to have a regular sed command inside my script. Could I do that instead?
Maybe your sed is in /bin or /usr/local/bin? The "hash-bang" line must specify the correct path, the kernel doesn't search for the command. But yes, you could just call sed from the script, i.e.
Code:
#!/bin/sh
exec sed -nf ...
(The exec is optional, but prevents having a shell hanging around just waiting for sed to exit.)
One last point, redirection is a little more efficient than cat, e.g.
Code:
sed_reverse < du1.txt
(Though I realise it was just an example, you could of course, avoid the tmp file and pipe the output of du straight to the script.)
Maybe your sed is in /bin or /usr/local/bin? The "hash-bang" line must specify the correct path, the kernel doesn't search for the command. But yes, you could just call sed from the script, i.e.
Code:
#!/bin/sh
exec sed -nf ...
(The exec is optional, but prevents having a shell hanging around just waiting for sed to exit.)
One last point, redirection is a little more efficient than cat, e.g.
Code:
sed_reverse < du1.txt
(Though I realise it was just an example, you could of course, avoid the tmp file and pipe the output of du straight to the script.)
When I pipe the output of du to
Code:
sed -nf 1! G p h
I get this error:
Quote:
sed: couldn't open file 1!: No such file or directory
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.