Script to accept Multi line inputs

Hi there,

I'm trying to create a script that will accept multiple inputs by copying and pasting the strings from a notepad, hit Enter key and output the string to a text file.I'm thinking of using the read command however it will just simply get the first line.

Apologies but got no idea how to start with.

Regards,
Norbie

Why not read the file directly from your script?

while read x
do
    process $x
done < /path/to/file.txt

Please post what you have and what went wrong.

Seems like s/he rather meant

while read x
  do  echo $x >textfile
  done
while IFS= read -r line
do
  printf "%s\n" "$line"
done > outfile

Finish the input by entering CTRL-D

Thank you for your help however these codes seem to work only when copying and pasting one by one.

echo "Enter string and hit Ctrl + D"
while IFS= read -r line
do
printf "%s\n" "$line"
done > tmp.log

for b in $(cat tmp.log)
do

find archive*/IT | xargs grep -l $b > count.log
VAR1="$(awk -F/ '{print $0}' count.log)"
echo "$b $VAR1"
done

I'm trying to create a scripts that will accept the following strings, search for it in a given directory and output the results.
Sample Input:

89234500
89234501
89234502

Sample Output:

89234500 ./archive02/IT/0012456.log
89234501 ./archive02/IT/0012457.log
89234502 ./archive02/IT/0012458.log

When I tried to copy the whole string inputs, this is what I got and not responding to Ctrl + D.

$ search.sh
Enter input and hit Ctrl + D








89234502

Appreciate your help on this. Thank you

You could do something like this. Put all the input in a file and grep using the file.

[user@host ~]$ cat file
89234500
89234501
89234502
[user@host ~]$ grep -f file -r archive*/IT/*
archive02/IT/0012456.log:89234500
archive02/IT/0012457.log:89234501
archive02/IT/0012458.log:89234502
[user@host ~]$

How about splitting the scipt into several blocks and executing them one by one so people (including you!) can tell WHAT goes wrong WHERE?

  • Is the tmp.log file created as expected?
  • Is the for loop executed for the contents of tmp.log?
  • Does the find commend supply its expected results?
  • Is the count.log file populated correctly?
  • Is the VAR1 variable created as expected (I guess, not, as the awk command substitution is quite pointless!)?

You have an error in your assessment of the problem: what you want is NOT "multiline input" but in fact you want your input one line at a time and work on each line separately. That is, if your input is:

line1
line2
line3
..

you first want to process "line1", then "line2" and so forth, yes?

You achieve this by reading one line at a time (the "echo" stands for any arbitrary processing you might want to do, replace it with a code block that does what you really want to do):

while read INPUT ; do
     echo "= $INPUT ="
done

Now save this to a file "script.sh" and call it this way:

$ ./script.sh < /path/to/input.file

and you will see the content of the file, one line at a time, but surrounded by equal signs (the proof that the script really has read and processed your file).

Note that it is good style to first "correct" the input: trim leading/trailing blanks, tabs, empty lines, maybe filter out comment lines. Suppose your input file looks like this:

# lines from project A
line-1
    line-2
# line-3   # obsolete

# lines from project B
line-4     
 line-5

What you probably want to process in fact is this:

line-1
line-2
line-4
line-5

therefore, refine your script like this ("<b>" is a literal blank, "<t>" a tab):

sed 's/#.*//;s/^[<b><t>]*//;s/[<b><t>]*$//;/^$/d' |\
while read INPUT ; do
     echo "= $INPUT ="
done

I hope this helps.

bakunin