Visit Our UNIX and Linux User Community


Shell script to search through numbers and print the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to search through numbers and print the output
# 1  
Old 10-29-2007
Shell script to search through numbers and print the output

Suppose u have a file like

1 30
ABCSAAHSNJQJALBALMKAANKAMLAMALK
4562676268836826826868268468368282972982
2863923792102370179372012792701739729291
31 60
HSJBSHSLJSLDINEINSLSLNSLWONSLJWLU
6238286298182720710730723978219021019302
7582687167391937972138892368963883R89942


Another file have the
32
44
22
27

So output shud like while going throgh the first file at those particular numbers we have
32 S 2 5
44 I 2 3
22 K 3 7
27 M 7 2

Last edited by cdfd123; 10-29-2007 at 02:37 PM.. Reason: As 30 shud be written above K and in the same way 60 shud be written above U
# 2  
Old 10-29-2007
explaining the selection logic (in 2 words or less) would help all of us!
also using the vB Codes when quoting the input files - could simplify the description.
# 3  
Old 10-29-2007
Quote:
Originally Posted by vgersh99
explaining the selection logic (in 2 words or less) would help all of us!
also using the vB Codes when quoting the input files - could simplify the description.
So i acn simplify question
1 10
SSSSKSAGG
342456784324
335678899000

so question is find what is at
8 A 3 0
1 S 3 3

Thanks
# 4  
Old 10-29-2007
Sorry, I don't understand it - must be me being 'thick-skulled'
# 5  
Old 10-29-2007
Quote:
Originally Posted by vgersh99
Sorry, I don't understand it - must be me being 'thick-skulled'
Yah sorry may be i can more simplfy i apologise

say one file is
1 5 positions denotes from 1 to 5
ASERF
12424
21342
where 12424 is just below their ASERF
and 21432 is just below 12424
another file is like numbers
2
3
4
that is position number

So at position number 2 is S 2 1
similarly at position 3 is E 4 3
similiarly at position 4 is R 2 4
# 6  
Old 10-29-2007
Try and adapt the following script :
Code:
awk '
NR==FNR {
   from = $1;
   to   = $2;
   for (i=1; i<=3; i++) {
      getline;
      for (j=from; j<=to; j++) {
         Values[j] = (i==1 && j==1 ? "" : Values[j] OFS) substr($0, j-from+1, 1);
      }
   }
   next;
}
{
   if ($1 in Values)
      print $1, Values[$1];
   else
      print $1, "Not Found";
}

' data_file numbers_file

data_file
Code:
1 30
ABCSAAHSNJQJALBALMKAANKAMLAMALK
4562676268836826826868268468368282972982
2863923792102370179372012792701739729291
31 60
HSJBSHSLJSLDINEINSLSLNSLWONSLJWLU
6238286298182720710730723978219021019302
7582687167391937972138892368963883R89942

numbers_file
Code:
1
2
22
30
31
32
44
60
99

Output
Code:
1 A 4 2
2  B 5 8
22  N 8 2
30  L 6 0
31  H 6 7
32  S 2 5
44  N 7 9
60  J 1 6
99 Not Found

Jean-Pierre.
# 7  
Old 10-30-2007
again same question with different inputs

Quote:
Originally Posted by aigles
Try and adapt the following script :
Code:
awk '
NR==FNR {
   from = $1;
   to   = $2;
   for (i=1; i<=3; i++) {
      getline;
      for (j=from; j<=to; j++) {
         Values[j] = (i==1 && j==1 ? "" : Values[j] OFS) substr($0, j-from+1, 1);
      }
   }
   next;
}
{
   if ($1 in Values)
      print $1, Values[$1];
   else
      print $1, "Not Found";
}

' data_file numbers_file

data_file
Code:
1 30
ABCSAAHSNJQJALBALMKAANKAMLAMALK
4562676268836826826868268468368282972982
2863923792102370179372012792701739729291
31 60
HSJBSHSLJSLDINEINSLSLNSLWONSLJWLU
6238286298182720710730723978219021019302
7582687167391937972138892368963883R89942

numbers_file
Code:
1
2
22
30
31
32
44
60
99

Output
Code:
1 A 4 2
2  B 5 8
22  N 8 2
30  L 6 0
31  H 6 7
32  S 2 5
44  N 7 9
60  J 1 6
99 Not Found

Jean-Pierre.

Dear Pierre,
If u have input with space in between like

1 8
A G H H H U I O
22 33 44 55 32 53 4 55

Another file
2
4
6
So output will be like
2 G 33
4 H 55
6 U 53

Last edited by cdfd123; 10-30-2007 at 02:30 PM.. Reason: modifying the input

Previous Thread | Next Thread
Test Your Knowledge in Computers #274
Difficulty: Easy
OpenSolaris is a descendant of the UNIX System V Release 4 (SVR4) code base developed by Sun and AT&T in the late 1980s.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need shell script to Telnet multiple node , Ping some IP and print output

Hi Team, Need shell script to Telnet multiple node , Ping some IP and print output like pass or fail. Need this script to check reachability of multiple nodes at same time. Help me. I use this but not working... Eg. in this script i need to telnet... (4 Replies)
Discussion started by: Ganesh Mankar
4 Replies

2. Shell Programming and Scripting

Need to Print output in table using shell script

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (4 Replies)
Discussion started by: ankit.mca.aaidu
4 Replies

3. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

4. Shell Programming and Scripting

How to print the output of a select query using shell script?

HI, I want to connect to database and fetch the count from a table. The sql query is as below : select count(*) from table_test where test_column='read'; How can I print the output of this statement using shell script. Thanks in advance. (4 Replies)
Discussion started by: confused_info
4 Replies

5. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

6. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

7. Shell Programming and Scripting

search file and print results with shell script

input file 1.<CRMSUB:MSIN=0100004735,BSNBC=TELEPHON-9814060328-TS11&TS21&TS22,NDC=9814,MSCAT=ORDINSUB,SUBRES=ALLPLMN-SPICE,BAOC=OIC,BAPRC=INFO,ACCSUB=BSS,NUMTYP=MULTI;... (3 Replies)
Discussion started by: dodasajan
3 Replies

8. Shell Programming and Scripting

how can i print the output of the shell script in bigger size

how can i print the output of the shell script in bigger size eg: echo " hello world" i want to print this in the output with bigger size in the middle of the screen. can someone please help me out in that (2 Replies)
Discussion started by: mail2sant
2 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies

Featured Tech Videos