edit a line for getting inputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting edit a line for getting inputs
# 1  
Old 02-21-2011
edit a line for getting inputs

Hello Everybody,

I have a file with data like this

Code:
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
Code:
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 do this

Thanks in advance!!!

Last edited by leo.maveriick; 02-21-2011 at 03:19 AM.. Reason: Inserting code template
# 2  
Old 02-21-2011
Hi,

Not sure about your output, but try this:
Code:
$ perl -aF: -ne '$input = join(":", @F); chomp $input; print "For $input", "; ", "a=", int($F[0]), ",", "b=", int($F[1]), "\n";' infile
For 8z:1; a=8,b=1
For 15y:0; a=15,b=0
For 7x:0; a=7,b=0
For 12w:1; a=12,b=1

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 02-21-2011
Try something like:
Code:
while IFS=: read a b
do
  a=${a%?}
  echo $a
  echo $b
done < file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 02-21-2011
Try something like...
Code:
$ echo '8z:1
> 15y:0
> 7x:0
> 12w:1' | while read my_row
> do
> eval $(echo $my_row|awk -F '[^0-9]:' '{print "a=" $1 ORS "b=" $2}')
> echo do something with $a and $b
> done
do something with 8 and 1
do something with 15 and 0
do something with 7 and 0
do something with 12 and 1

This User Gave Thanks to Ygor For This Post:
# 5  
Old 02-21-2011
here is the basic logic . here you can get two variables $a and $b for every line. You can modify it as per your requirement

PHP Code:
while read line
do
a=`echo $line |awk -F: '{print  $1 }'`
b=`echo $line |awk -F: '{print  $2 }'`
a=`echo $a | tr -d '[:alpha:]'`
b=`echo $b | tr -d '[:alpha:]'`
echo 
"a=$a,b=$b"

done $inputfilename 
This User Gave Thanks to amitranjansahu For This Post:
# 6  
Old 02-21-2011
Options working great..Thanks everyone!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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... (7 Replies)
Discussion started by: norbie.lopez
7 Replies

2. Shell Programming and Scripting

[Solved] How to refer more than 9 command line inputs for a scripts in korn shell?

Hi all, I have a script which should take more than 9 command line inputs while running. Likescript.sh a s d f g h j j k l o p i u y t r e w Now in the script if I have to access one of the input which is at position after 9, in this case say 'p' then how can I do that? echo $12 will not work... (15 Replies)
Discussion started by: pat_pramod
15 Replies

3. Shell Programming and Scripting

Edit first line of a text file

Hi friends, Issue1: I have a text file with the first line like this #chrom start end Readcount_A Normalized_Readcount_A ReadcountB Normalized_Readcount_B Fc_A_vs_B pvalue_A_vs_B FDR_A_vs_B Fc_B_vs_A pvalue_B_vs_A FDR_B_vs_A <a href="http://unix.com/">Link</a> How can I change it to the... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

4. Shell Programming and Scripting

Edit a Huge one line file

We have a huge file which has just one really large line; about 500 MB. I want to 1. Count all the occurrences of a phrase 2. Replace the phrase with another. Trying to open it using vi has not helped as it complains that it is too large. Can any script help? Please advise. Thank you, (12 Replies)
Discussion started by: kaushikadya
12 Replies

5. Shell Programming and Scripting

How to edit file to have one line entry?

Hello All, My file content is: DROP TABLE "FACT_WORLD"; CREATE TABLE "FACT_WORLD" ( "AR_ID" INTEGER NOT NULL, "ORG_ID" INTEGER NOT NULL ) DATA CAPTURE NONE COMPRESS YES; I want to change this file to have entries in one... (6 Replies)
Discussion started by: akash2508
6 Replies

6. Shell Programming and Scripting

edit and add line

dear all, i need your help to change this input to output M9_3D_H10__Dflt ->SP_M9N_S 497224.3125 1598028.1250 497063.2813 1598002.7500 496953.1250 1597951.8750 497122.6250 1597985.7500 497190.4375 1597994.2500... (3 Replies)
Discussion started by: ipatah
3 Replies

7. Shell Programming and Scripting

Edit a line in a file with perl

Hi, How can I edit a line in a file? For example, a.txt contains: start: 1 2 3 4 stop: a b c d and I want to change "3" to "9" and to add "5" after "4" the result should be (a.txt): start: 1 9 3 4 5 stop: a b c d Thanks, zed (5 Replies)
Discussion started by: zed
5 Replies

8. UNIX for Dummies Questions & Answers

edit each line in the file

I am trying to edit each line in a file. The file has several columns delimitted by '|'. I need to take out the last two columns. Each line starts with a unique word through which I am storing the lines in a variable and cutting the last two colums. But, when I am echoing the line, it is... (2 Replies)
Discussion started by: chiru_h
2 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