Bash - 2 arguments - bash

How do you insert 2 arguments at the same time? so far I could only insert the $filename but
not the $Number of files I want at the same time.
this is the prompt:
"Enter the file name followed by the amount of that file you want"
grep $filename input.txt|head -n$Number;;

I am not following you. Please correct your question.
But most probably you want this:
PROMPT_TEXT='Enter the filename followed by the amount of that file you want: '
read -p "$PROMPT_TEXT" filename amount
grep -m "$amount" -- "$filename" input.txt

Related

BASH - Check if file exists and if it does, append the filename to a .txt

I'm trying to create a bash script that first looks for a name and then checks whether a certain filename, for example vacation021.jpg exists in the file system, and if it exists I want to append the filename to a .txt file.
I'm having a lot of issues with this, I'm still very new to bash.
This is as far as I've gotten.
> oldFiles.txt
files=$(grep "jane " list.txt)
for i in $files; do
if test -e vacation021.jpg;
then echo $i >> oldFiles.txt; fi
done
This however appends all the separate words in the list.txt to the oldFiles.txt.
Any help would be much appreciated.
for i in $files will iterate over each word in $files, not the lines. If you want to iterate over the lines, pipe the output of grep to the loop:
grep 'jane ' list.txt | while read -r i; do
if test -e vacation021.jpg
then printf "%s" "%i"
fi
done > oldFiles.txt
But as mentioned in the comments, unless the vacation021.jpg file is going to be created or deleted during the loop, you can simply use a single if:
if test -e vacation021.jpg
then
grep 'jane ' list.txt > oldFiles.txt
fi

bash - combine while read with grep and cut

I want to modify my existing bash script. This is how it looks now:
#! /bin/bash
SAMPLE = myfile.txt
while read SAMPLE
do
name = $SAMPLE
# some other code
done < $SAMPLE
In this case 'myfile'.txt consists only of one column, with all the info I need.
Now I want to modify this script because 'myfile.txt' contains now more columns and more lines than I need.
grep 'TEST' myfile.txt | cut -d "," -f 1
gives me the values I need. But how can I integrate this into my bash script?
You can pipe the output of any command into a while read loop.
Try this:
#! /bin/bash
INPUT=myfile.txt
grep 'TEST' $INPUT |
cut -d "," -f 1 |
while read SAMPLE
do
name=$SAMPLE
# some other code
done
You have to change the input field separator (IFS), which tells read where to split the input line. Then you tell read to read two fields: the one you need and one you do not care about.
#! /bin/bash
SAMPLE=myfile.txt
while IFS=, read SAMPLE dontcare
do
name="$SAMPLE"
# some other code
done < <(grep TEST "$SAMPLE")
By the way: whenever you use read, you should consider to use the option -r.

How to display a word from different lines?

I wanna be able to display every line that has the word from input read.
Now Im able to display the index position of the input read word.
echo "Filename"
read file
echo "A word"
read word
echo $file | awk '{print index($0,"'"$word"'")}'
as requested here is an example of how you could use grep.
The following command will use wget to fetch the content of this stackoverflow webpage, the -O - option tells wget to output the html into std out, which then gets piped into grep -n grep.
grep matches the number of instances the term "grep" appears in the html and then outputs with the corresponding line number where the match occurs - and highlights the match.
wget -O - http://stackoverflow.com/questions/27691506/how-to-display-a-word-from-different-lines | grep -n grep
e.g. When I run in my terminal (ignoring the wget related output) grep -n grepgives:
42: <span class="comment-copy">why dont you use <code>grep</code> ?</span>
468: <span class="comment-copy">#nu11p01n73R Can you give me any example, on how to use grep?</span>
495: <span class="comment-copy">As per your question <code>grep $word $file</code> will output lines in <code>$file</code> containing <code>$word</code></span>
521: <span class="comment-copy">If you want line numbers too, you can use <code>grep</code> like this: <code>grep -in $word $file</code></span>
Note stackoverflow syntax highlighting is different to what you get in terminal
If you want to print also the line number
read -p 'Filename? ' fname
read -p 'Word to search? ' word
awk /"$word"/'{print NR, $0}' "$fname"
If you don't want the line number
read -p 'Filename? ' fname
read -p 'Word to search? ' word
awk /"$word"/ "$fname"
BTW, what everyone told you is "use grep", but look at this
% time for i in {1..10000} ; do grep alias .bashrc > /dev/null ; done
real 0m22.665s
user 0m2.672s
sys 0m3.564s
% time for i in {1..10000} ; do mawk /alias/ .bashrc > /dev/null ; done
real 0m21.951s
user 0m3.412s
sys 0m3.636s
Of course gawk is slower, in this case 27.9 seconds.

Unix shell to prompt user for filename

please pardon since I am a UNIX beginner.
I wanna write a shell script that can ask user type in the filename and output the number of lines in that file, here is my code:
echo "Pls enter your filename:"
read filename
result=wc -l $filename
echo "Your file has $result lines"
However, I couldn't get it working since it complains about the identifier "filename". Could experts help?Thanks !!
That works fine, at least in bash. Well, the read works fine. However, the assignment to result should probably be:
result=$(wc -l $filename)
but, since that command outputs both the line count and the filename, you might want to change it a little to just get the line count, something like:
result=$(cat $filename | wc -l)
or:
result=$(wc -l <$filename)
The command you have:
result=wc -l $filename
will set result to the literal wc, then try to execute the -l command.
For example, the following five-line script:
#!/bin/bash
echo "Pls enter your filename:"
read filename
result=$(cat $filename | wc -l)
echo "Your file has $result lines"
will, when run and given its name as input, produce the following:
Your file has 5 lines
If you're not using bash, you need to specify which shell you are using. Different shells have different ways of doing things.
If you want to evaluate wc -l $filename , you have t do:
result=$(wc -l $filename)
Otherwise, with result=wc -l $filename, bash will assign wc to result, and interpret the next word(-l) as a command to run.
Here you go :
echo "Pls enter your filename:"
read filename
result=`wc -l $filename | tr -s " " | cut -f2 -d' '`
echo "Your file has $result lines"

Help with Bash script

I'm trying to get this script to basically read input from a file on a command line, match the user id in the file using grep and output these lines with line numbers starting from 1)...n in a new file.
so far my script looks like this
#!/bin/bash
linenum=1
grep $USER $1 |
while [ read LINE ]
do
echo $linenum ")" $LINE >> usrout
$linenum+=1
done
when i run it ./username file
i get
line 4: [: read: unary operator expected
could anyone explain the problem to me?
thanks
Just remove the [] around read line - they should be used to perform tests (file exists, string is empty etc.).
How about the following?
$ grep $USER file | cat -n >usrout
Leave off the square brackets.
while read line; do
echo $linenum ")" $LINE
done >> usrout
just use awk
awk -vu="$USER" '$0~u{print ++d") "$0}' file
or
grep $USER file |nl
or with the shell, (no need to use grep)
i=1
while read -r line
do
case "$line" in
*"$USER"*) echo $((i++)) $line >> newfile;;
esac
done <"file"
Why not just use grep with the -n (or --line-number) switch?
$ grep -n ${USERNAME} ${FILE}
The -n switch gives the line number that the match was found on in the file. From grep's man page:
-n, --line-number
Prefix each line of output with the 1-based line number
within its input file.
So, running this against the /etc/passwd file in linux for user test_user, gives:
31:test_user:x:5000:5000:Test User,,,:/home/test_user:/bin/bash
This shows that the test_user account appears on line 31 of the /etc/passwd file.
Also, instead of $foo+=1, you should write foo=$(($foo+1)).

Resources