Script to accept Multi line inputs

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Script to accept Multi line inputs
# 8  
Old 12-27-2016
Quote:
Originally Posted by norbie.lopez
Thank you for your help however these codes seem to work only when copying and pasting one by one.

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

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:

Code:
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):

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

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

Code:
$ ./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:

Code:
# 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:

Code:
line-1
line-2
line-4
line-5

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

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

I hope this helps.

bakunin
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