Hi all,
I'm new to shell scripting and hence this query.
I have 2 files. temp.txt and config.txt.
The values in temp.txt are tab separated.
ex: temp.txt
AB CDE GHIJ OPQRS WXY
ex:config.txt (1st line for 1st element of temp.txt and so on)
start = '1' end='5'
start = '6' end = '10'
start= '11' end ='15'
and so on....
i have to create an output such that each word of temp.txt is inserted into a new file main.txt with starting position as specified in config.txt.
like position 1-5 shud have the 1st word i.e 1st position 'A' 2nd position 'B' and position 3-5 shud be blank. C shud start from position 6.
awk 'NR == FNR {
/^[ \t]*\47name/ && c++ # get the field number
if (/^[ \t]*\47size/) {
split($0, t, ":")
gsub(/[ \t\47,]/, x, t[2]) # strip punctuation
fmt[c] = t[2] # get the size
}
next # run the above actions
} # + only for the first input file
{
for (i=1; i<=NF; i++) # output the strings in the correct format
printf "%" (length($i) > fmt ? "." : "-" ) fmt "s", $i
print x
}' config.txt temp.txt
Cuold you please post (copy/paste) the real input data (only if different from what you already provided), the exact command that you' re executing on HP-UX and the output you' re getting?
This is the real input and output i'm trying rt now.
no change whatever i had pasted earlier.
Example 1:
I/p is
1 12 123 12 34 1
A AB ABC ABDC e
OUTPUT in HP-UX is as follows
1 12123 12341
A ABABC ABDCe
Expected/IBM output is
1 12123 1234 1
A ABABC ABDC e
Example 2:
I/p is
1 123 12 34 1
A AB ABC ABDC e
cat -t tab.txt
1^I^I123^I12 34^I1
A^IAB^IABC^IABDC^Ie
HP o/p:
1 1212 341
A ABABC ABDCe
IBM/expected o/p
1 123 12 34 1
A AB ABC ABDC e
It's because the implicit data type conversion is different across different AWK implementations. Try this code:
awk 'NR == FNR {
/^[ \t]*\47name/ && c++ # get the field number
if (/^[ \t]*\47size/) {
split($0, t, ":")
gsub(/[ \t\47,]/, x, t[2]) # strip punctuation
fmt[c] = t[2] # get the size
}
next # run the above actions
} # + only for the first input file
{
for (i=1; i<=NF; i++) # output the strings in the correct format
printf "%" (length($i) > fmt + 0 ? "." : "-" ) fmt "s", $i
print x
}' config.txt temp.txt
As I said, you need to explicitly set the field separator if it's differnet than the default one:
awk -F'\t' 'NR == FNR {
/^[ \t]*\47name/ && c++ # get the field number
if (/^[ \t]*\47size/) {
split($0, t, ":")
gsub(/[ \t\47,]/, x, t[2]) # strip punctuation
fmt[c] = t[2] # get the size
}
next # run the above actions
} # + only for the first input file
{
for (i=1; i<=NF; i++) # output the strings in the correct format
printf "%" (length($i) > fmt + 0 ? "." : "-" ) fmt "s", $i
print x
}' config.txt temp.txt