Click to See Complete Forum and Search --> : [Homework?] Complete NOOB need help


gham
06-05-2007, 07:25 AM
I am a complete NOOB to linux and shell scripting and need help

this is what I am trying todo

1. Create a script that takes 1 argument being a file read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.

my efforts sofar

#!/bin/bash
echo "filename"
read myvar
grep $USER $myvar > results.txt
nl results.txt


2. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the middle (middle line) of the file named in second argument.

Thanks in advance for any help

deathadder
06-05-2007, 08:25 AM
Hi,

Have a read of the Posting Guidelines (http://www.justlinux.com/forum/showthread.php?t=91074).

This isn't home work is it ;)

Have a look at The Linux Documentation Project:

TLDP : Guides (http://www.tldp.org/guides.html)
Bash Guide for Beginners (http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html)
Advanced Bash-scripting Guide (http://www.tldp.org/LDP/abs/html/index.html)

To see if they can help you at all.


Your very close to having the first one working properly, '>' redirects and overwrites a file, as you've found out. Have a look through the bash guides, or a google for 'bash file redirection'

One thing you might think about doing:

read your results file, and loop through each line, formatting the output the way you want and redirecting it to another file.

gham
06-05-2007, 08:41 AM
thanks for the reply ... and NO it's not homework ... :)

deathadder
06-05-2007, 08:50 AM
Had to ask, the number of homework questions is quiet large at times, and to be fair it sounds like one ;)

I don't think you can format the output of nl the way you want. A while statement is possibly your best way to go...

count = 1
while read line from results.txt do
print $count) $line > anotherfile.txt
count++
done
mv anotherfile.txt results.txt

lagdawg
06-05-2007, 10:54 AM
This should do close to what you need it to for the first part:


sed "s/$USER/[&]/g" $myvar | grep $USER | nl >results.txt

ghostdog74
06-05-2007, 12:09 PM
well, the way you ask the questions, it is 99.99% homework to me..but nevertheless there's still 0.01% benefit of the doubt i can give to you.

1. Create a script that takes 1 argument being a file read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.


USER=$1 #first argument
awk '/'"$USER"'/{print NR " " $0 > "results" }' "file"

it says take the input "file", match the user and bring the record number, and that matched line into "results" file.


2. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the middle (middle line) of the file named in second argument.

if your file is not very large..this is one way..

awk -v text="$1" -v file="$2" '{arr[++c]=$0}
END{
for(i=1;i<=c/2;i++){
print arr[i] > file
}
print text > file
for(i=c/2+1;i<=c;i++) {
print arr[i] > file
}
}' "file"

gham
06-05-2007, 01:07 PM
yep these all work.... except I cannot use AWK or SED .

and as I have said this is NOT homework .... it's an Adult education course I am doing.

deathadder
06-05-2007, 01:36 PM
Wouldn't that make it homework ;)

Why can't you use awk and sed?

deathadder
06-05-2007, 01:42 PM
#!/bin/bash
echo "filename"
read myvar
grep $USER $myvar > results.txt
x=1
cat results.txt | while read line
do
echo "$x) $line" >>results2.txt
x=$((x+1))
done
Should do what you need, it's not nice though, without using sed or awk.

WhiteKnight
06-05-2007, 02:33 PM
well... its still homework in a way right? just that its not going graded maybe?:p

ph34r
06-05-2007, 02:42 PM
At least I know you aren't one of my students... we don't start shell scripting until tonite's class :)

ghostdog74
06-05-2007, 07:34 PM
#!/bin/bash
echo "filename"
read myvar
grep $USER $myvar > results.txt
x=1
cat results.txt | while read line
do
echo "$x) $line" >>results2.txt
x=$((x+1))
done
Should do what you need, it's not nice though, without using sed or awk.
catting a file and piping to while loop is unnecessary because the while loop itself can take in redirection. it can be shortened to

while read line
do
....
done < $myvar

or just

...
grep $USER $myvar | while read line
....

webwolf
06-06-2007, 01:49 AM
why not have a look at fgrep?? That should trim it down to about 2-3 line of code

deathadder
06-06-2007, 02:08 AM
Ah thanks for letting me know that ghostdog74, my scripting is in need of improvement as you can tell ;)