I have two files:
One is a list of numbers:
1
5
6
7
10
The second is a very long string
abcdefghijklmno......1234567890
I'd like to print a new file with a 5 character substring that starts at each the positions listed in the 1st file:
such as:
abcde
efghi
fghij
ghijk
jklmno
I've attempted a few While and If loops but can't seem to get the syntax correct.
Thank you.
Next time post your code, like that we can see where exactly are you stuck?
Here is the solution using awk.
awk '
NR==FNR{
a[NR]=$0
next
}
{
for(i=1;i<NR;i++)
print substr($0,a,5)
}
' file1 file2 > file3
Thanks so much that works great. I'm sorry about not posting my code. I'm new. I'll post the code next time.
Cheers!
No need to use awk. Can be all done within your shell if using a modern shell such as ksh93, bash or zsh.
#!/usr/bin/ksh93
while read pos; do
print ${1:$pos-1:5}
done < file1
No need to use awk. Can be all done within your shell if using a modern shell such as ksh93, bash or zsh.
#!/usr/bin/ksh93
read file2
while read pos; do
print ${file2:$pos-1:5}
done < file1
cat file2 | ./shellscript
or with no use of cat
./shellscript < file2