Operating System - HP-UX
1831277 Members
2758 Online
110022 Solutions
New Discussion

Re: Small help in shell script

 
SOLVED
Go to solution
Sreekanth Makam
Occasional Advisor

Small help in shell script

Hi folks,

I have 2 files..lets take A and B
I want a shell script such that, it must read first line from file "A" and 2 line from "B". This must repeat for whole file a.

I think you are confused..Let me explain with an example...

"A" file containes
1
2
3
and "B" file containes
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
now i want output like

1 aaa
1 bbb
2 ccc
2 ddd
3 eee
3 fff
4 ggg
4 hhh


This is very importaant and urgent...Please friends help me fast...

Thnks in advance

***** Problem is a problem untill U feel that is a Problem****
*** Let us share the knowledge ***
12 REPLIES 12
H.Merijn Brand (procura
Honored Contributor

Re: Small help in shell script

does it have to be a *sh* script?

# perl -le'$b=pop;@a=<>;@ARGV=($b);@b=<>;for(@a){print$_,splice@,0,3}' fileA fileB

will do it. No error checks on fileb having at least 3 times the lines fileA has

There are several variants to this of course. Above looked the shortest

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor
Solution

Re: Small help in shell script

cat fileB |&

cat fileA |
while read varA
do
read -p varB
print "$varA $varB"
read -p varB
print "$varA $varB"
done

of course there is no error checking that the files will have matching entries, etc.
Tim D Fulford
Honored Contributor

Re: Small help in shell script

I like the perl, how about this as an alternative
# awk '{printf "%s\n%s\n", $0, $0}' A > AA
# paste AA B

Tim
-
H.Merijn Brand (procura
Honored Contributor

Re: Small help in shell script

And I misread the post just a bit. Here's a modified snippet

# perl -e'$b=pop;chomp(@a=<>);@ARGV=($b);@b=<>;for$a(@a){print"$a $_"for splice@b,0,3}' fileA fileB

Lets test ...

lt09:/home/merijn 126 > perl -e'$b=pop;chomp(@a=<>);@ARGV=($b);@b=<>;for$a(@a){print"$a $_"for splice@b,0,3}' fileA fileB
1 aaa
1 bbb
1 ccc
2 ddd
2 eee
2 fff
3 ggg
3 hhh
lt09:/home/merijn 127 >

Sorry if that caused confusion
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Tim D Fulford
Honored Contributor

Re: Small help in shell script

procura...

I get the following eror for:
# perl -le '$b=pop;@a=<>;@ARGV=($b);@b=<>;for(@a){print $_,splice @,0,3}' A B
Number found where operator expected at -e line 1, near "@,0"
(Missing operator before 0?)
syntax error at -e line 1, near "@,0"
Execution of -e aborted due to compilation errors.

Tim
-
H.Merijn Brand (procura
Honored Contributor

Re: Small help in shell script

repaired in last post. (And checked this time)

Enjoy, Have FUN!H.Merijn
Enjoy, Have FUN! H.Merijn
Tim D Fulford
Honored Contributor

Re: Small help in shell script

I think procura deserves a ..10.. BUT the code is still wrong (see question, it asks repeat two lines from A for each line of B not 3...) that said, it is trivial to fix..

# perl -e'$b=pop;chomp(@a=<>);@ARGV=($b);@b=<>;for$a(@a){print"$a $_"for splice@b,
0,2}' A B

did the trick for me.. excellent much neater than my "awk & paste"

Tim
-
Sreekanth Makam
Occasional Advisor

Re: Small help in shell script

Hi curt,

It helped me a lot.. Thnks ....

Thnks for others also..But I don't know perl. But after seeing your answers..I feel perl is better and faster.


***** Problem is a problem untill U feel that is a Problem****
*** Let us share the knowledge ***
H.Merijn Brand (procura
Honored Contributor

Re: Small help in shell script

`better' is not very objective for scripts. There is a good language for every solution in unix, and it's not always Perl.

The more you are familiar with /any/ language, the more you will use it, and the faster it will bring you to the desired results.

I know people that wrote 2000 line sys-admin scripts in csh. I bet that there are more people on this forum that shivver to maintain that than there are people that shivver to decrypt one of my perl one-liners :)

I know perl, because I use it for almost everything, but until you are familiar with it, awk, sed, sh, sort, and comm are proably better utilt for you than Perl. Even if the given solution is faster and/or `better'.

I was tired yesterday, obviously. the typo (missing 'b') in my first post is due to my keyboard, that often needs a harder hit for the 'b' key, but the misreading of your post, and the second misreading of 3 instead of two is simply sloppy and careless.

Perl is a fine language for scripting, because it is so versatile, and has all the needed glue elements built-in. I can understand people not using it for bigger projects due to the sometimes obfuscated constructs, but IMHO there also lies the grace of it. Perl is much more powerful and versatile than Python for example, but in larger development groups, I can imagine that the group benefits from the restrictive formats and structures of Python.

I, for example, never use posix-sh features, but restrict myself, when programming sh, to the very old bourne shell set just because of (backward) compatibility.

Enjoy (scripting), Have FUN! H.Merijn [ always there to promote Perl ]
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: Small help in shell script

Hai.

Attached script will do the requirement. But b file has to contain twice the lines as in a file. If you don't want to check this remove the checking loop in script.

Give the input files and result file,you will get the required results on result file.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Sreekanth Makam
Occasional Advisor

Re: Small help in shell script

Hello muthukumar,

U wrote a really big program..Sorry yaar..I did'nt like it..Shell scrip must me sweet and short..

Any way thnks for your replay...
*** Let us share the knowledge ***
H.Merijn Brand (procura
Honored Contributor

Re: Small help in shell script

For your reference: http://developers.slashdot.org/article.pl?sid=04/06/12/2125229

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn