-
Complete NOOB need help
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
Last edited by gham; 06-06-2007 at 03:36 AM.
-
Hi,
Have a read of the Posting Guidelines.
This isn't home work is it
Have a look at The Linux Documentation Project:
TLDP : Guides
Bash Guide for Beginners
Advanced Bash-scripting Guide
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.
Last edited by deathadder; 06-05-2007 at 08:35 AM.
-
thanks for the reply ... and NO it's not homework ...
-
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...
Code:
count = 1
while read line from results.txt do
print $count) $line > anotherfile.txt
count++
done
mv anotherfile.txt results.txt
Last edited by deathadder; 06-05-2007 at 08:52 AM.
-
This should do close to what you need it to for the first part:
Code:
sed "s/$USER/[&]/g" $myvar | grep $USER | nl >results.txt
-
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.
Originally Posted by gham
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.
Code:
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..
Code:
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"
Last edited by ghostdog74; 06-05-2007 at 12:12 PM.
-
Thanks guys
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.
Last edited by gham; 06-05-2007 at 01:09 PM.
-
Wouldn't that make it homework
Why can't you use awk and sed?
-
Code:
#!/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.
Last edited by deathadder; 06-05-2007 at 01:48 PM.
-
well... its still homework in a way right? just that its not going graded maybe?
Registered Linux User #388117
-
At least I know you aren't one of my students... we don't start shell scripting until tonite's class
-
Originally Posted by deathadder
Code:
#!/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
Code:
while read line
do
....
done < $myvar
or just
Code:
...
grep $USER $myvar | while read line
....
-
why not have a look at fgrep?? That should trim it down to about 2-3 line of code
-
Ah thanks for letting me know that ghostdog74, my scripting is in need of improvement as you can tell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|