1846130 Members
4998 Online
110254 Solutions
New Discussion

file sorting question

 
SOLVED
Go to solution
steven Burgess_2
Honored Contributor

file sorting question

Hi all

easy one here, its 4:00am and I can't seem to think!!

I have a file that contains

123
456
789

etc etc

I need to read the file and output

c1t2d3

etc etc

My problem is I have no delimeter and am struggling

cheers

Steve
take your time and think things through
9 REPLIES 9
Patrick Wallek
Honored Contributor
Solution

Re: file sorting question

Try the cut command. I'm not sure if I'll get the syntax correct but you should be able to figure it out.

for i in $(cat file)
do
C=$(echo ${i} | cut -c 1)
D=$(echo ${i} | cut -c 2)
T=$(echo ${i} | cut -c 3)
DEVICE=c${C}d${D}t${T}
done

I think that should work.

Good luck
Patrick Wallek
Honored Contributor

Re: file sorting question

Oops...I ommitted the echo from the little script. Before the done statement put in:

echo ${DEVICE}

I just did a quick test and that does work.
steven Burgess_2
Honored Contributor

Re: file sorting question

Cheers Patrick

should have thought of that really as I am using a cut in the preceding pipe to get the above info...doh!

Steve
take your time and think things through
Sridhar Bhaskarla
Honored Contributor

Re: file sorting question

Steve,

My 2 cents.

If you are the one who is generating the input file, then I suggest you add a delimiter for c,t and d values. For ex., if you are making a file using the device files c1t2d3, c2t10d4 and c12t0d1, then your input file will become

123
2104
1201

It's just difficult to get back the original device files from the above. Where in, if you put a delimiter like :
1:2:3
2:10:4
12:0:1

It's really easy to get back the original device files.

I don't know your requirements but I am just cautioning for such cases in future.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
curt larson_1
Honored Contributor

Re: file sorting question

how about

for i in $(cat file)
do
device=$(echo ${i} | sed -e 's/\(.\)\(.\)\(.\)/c\1t\2d\3/')
print $device
done
Mark Grant
Honored Contributor

Re: file sorting question

perl -ne '($a,$b,$c)=split //;print "c${a}d${b}t${c}\n"' datafile

I had to offer this perl version if only so that I can learn something when Merijn pops in and does it using some perl construct I've never heard of.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: file sorting question

Or use sed:

sed 's/\(.\)\(.\)\(.\)/c\1t\2d\3/g'

Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: file sorting question

Oops, I missed the fact that someone else also gave the sed solution.
But mind: you don't have to do the for loop. Just do: cat $file | sed ...
Or even:
sed ... < $file

One thing to bear in mind: it only will work with target ID's below 10.

If you have large target id's, the length of the string can be 3 or 4 characters, meaning the sed statement has to be changed:
cat $file | sed 's/^\(.\)\(.*\)\(.\)$/c\1t\2d\3/'

Which will put the first and last character in the buffers 1 and 3 and all characters in between in buffer 2. That is also why you will need the carrot and dollar sign now.
Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: file sorting question

And wheely got me. I know of several ways to do it, but this sed is just simply the easiest and fasted way

playing golf, one can loose the anchors

# sed 's/\(.\)\(.*\)\(.\)/c\1t\2d\3/'
in perl one can loose more characters, but in basics it is the same

# perl -pe's/(.)(.)(.)/c$1t$2d$3/' file

obfuscation only makes it longer. But if you *know* that each line always is ^\d\d\d$, you can play a wicked game by dropping the last capture:

# perl -pe's/(.)(.)/c$1t$2d/' file

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