Script to accept Multi line inputs

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Script to accept Multi line inputs
# 1  
Old 12-26-2016
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
# 2  
Old 12-26-2016
Why not read the file directly from your script?

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

# 3  
Old 12-26-2016
Please post what you have and what went wrong.

Seems like s/he rather meant
Code:
while read x
  do  echo $x >textfile
  done

# 4  
Old 12-26-2016
Code:
while IFS= read -r line
do
  printf "%s\n" "$line"
done > outfile

Finish the input by entering CTRL-D
# 5  
Old 12-27-2016
Thank you for your help however these codes seem to work only when copying and pasting one by one.

Code:
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:
Code:
89234500
89234501
89234502

Sample Output:
Code:
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.

Code:
$ search.sh
Enter input and hit Ctrl + D








89234502

Appreciate your help on this. Thank you

Last edited by norbie.lopez; 12-27-2016 at 06:57 AM.. Reason: updates
# 6  
Old 12-27-2016
You could do something like this. Put all the input in a file and grep using the file.

Code:
[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 ~]$

# 7  
Old 12-27-2016
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!)?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

Slack message multi line from UNIX script

Hi OS: Redhat Version 7.5 Enterprise Trying to post message from shell script to Slack channel and trying below code: text="$msg" text1="$lmsg" if ] then echo "No text specified" exit 1 fi escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )... (13 Replies)
Discussion started by: onenessboy
13 Replies

3. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

4. Shell Programming and Scripting

Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider: sed -e '/^3/d' -e '$ a text' supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts... (2 Replies)
Discussion started by: msz59
2 Replies

5. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

6. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

7. Shell Programming and Scripting

edit a line for getting inputs

Hello Everybody, I have a file with data like this 8z:1 15y:0 7x:0 12w:1 ... ... I would like read each line through a loop and will needing to get the input from each line like this For 8z:1; a=8,b=1 15y:0; a=15,b=0 7x:0; a=7,b=0 Please let me know of a way to... (5 Replies)
Discussion started by: leo.maveriick
5 Replies

8. Shell Programming and Scripting

Multi line variable script... needs help.

I am trying to write a script that will help me put a file into excel with little manipulation. Below is a sample of the file im using. Group1:*:gid1:user,user Group2:*:gid2:user,user Group3:*:gid3:user,user,user,user,user,user,user Group4:*:gid4:user,user I marked in red the part that is... (1 Reply)
Discussion started by: rookieuxixsa
1 Replies

9. Shell Programming and Scripting

Vaildation of command line inputs

Hi, I want to do the following validations in my script when my script gets 2 parameters as command line inputs. My script expects 2 inputs : a -f option and a filename If a filename is given as input without the -f option then I have to exit. If only -f option is given and no filename is... (6 Replies)
Discussion started by: sendhilmani123
6 Replies

10. Shell Programming and Scripting

Command line inputs validation

Hi, I have a script called read.sh that takes a file as input. Now I want to make that script take the file as input with a -f option preceding the filename. How can I do this validation. How can I find whether the given option is -f or not inside the script. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies
Login or Register to Ask a Question