Operating System - HP-UX
1833861 Members
2397 Online
110063 Solutions
New Discussion

can sed search for last two digits of line...

 
SOLVED
Go to solution
Ratzie
Super Advisor

can sed search for last two digits of line...

I have a huge file with a bunch of data.
2088872167,15 1 01 047
similar
similar

Nothing is consistent except for the spacing.
I need to insert a space before the last two digits of the line...
2088872167,15 1 01 0 47

Can this be done
What would be the syntax...
Thanks
12 REPLIES 12
Alan Meyer_4
Respected Contributor

Re: can sed search for last two digits of line...

to get the line you specified enter;

grep '47$' filename

Like the $ specifies to find the pattern at the end of a line.
" I may not be certified, but I am certifiable... "
Ratzie
Super Advisor

Re: can sed search for last two digits of line...

I need this done to every line in my file
From this:
2088872167,15 1 01 047
2088872168,15 1 01 123
2088872166,15 1 01 546
2088872164,15 1 01 048
...
...

To this:
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48
...
...
Alan Meyer_4
Respected Contributor

Re: can sed search for last two digits of line...

sorry I missread your request...
" I may not be certified, but I am certifiable... "
Mel Burslan
Honored Contributor

Re: can sed search for last two digits of line...

while reading the following solution, please bear in mind that my perl programming experience is next to nil. Having said that,

create a little script called L.pl as follows

#!/usr/bin/perl
$a=;
$l=length $a;
printf $l

(if your perl executable is in a different location, modify the first line accordingly)

chmod 755 L.pl

lets say your filename is MYFILE

cat MYFILE | while read line
do

len=`echo $line | ./L.pl`
let m2=$len-3
let m1=$len-2

part1=`echo $line | cut -c 1-${m2}`
part2=`echo $line | cut -c ${m1}-${len}`

echo $part1" "$part2 >> MY_new_FILE
done


again, this is coming from someone who can barely find his way through the infamous perl camelbook. I am sure there are more elaborate solutions using perl to to do the same in one command.

if your data on every line is a fixed length, you do not need the perl portion at all. Just replace that line with

len=your_line_length+1
(+1 because in perl first position is 0 not 1)

then you should be okay

Hope this helps
________________________________
UNIX because I majored in cryptology...
Vibhor Kumar Agarwal
Esteemed Contributor

Re: can sed search for last two digits of line...

Try this

sed -e 's/\(.*\)../\1 /g' file

will give the output of
2088872167,15 1 01 047
as
2088872167,15 1 01 0 (space)

sed -e 's/\(.*\) \(.*\)/\1/g'
will give the last coloumn
047

Now what you have to do is simply take the last coloumn get the last 2 bits and append it.

sed -e 's/\(.*\) \(.*\)/\1/g' | cut -c 2,3
will give 47

Now just append this to the end of line.
This is general also, and will help with every line.
Vibhor Kumar Agarwal
curt larson_1
Honored Contributor

Re: can sed search for last two digits of line...

sed -e 's/\(..\)$/ \1/' file
Vibhor Kumar Agarwal
Esteemed Contributor

Re: can sed search for last two digits of line...

Wow Curt,

I was doing a page full of scripting for that.
Vibhor Kumar Agarwal
curt larson_1
Honored Contributor

Re: can sed search for last two digits of line...

just to be sure you know this:

sed -e 's/\(..\)$/ \1/' file
^^^^^^^^^^^^^^^^^^blank/space before \1
curt larson_1
Honored Contributor

Re: can sed search for last two digits of line...

vibhor, you had most of it already.
next time will be easier.

and it is a short sed script, but doing it in the shell is probably faster

while read var
do
firstPart=${var%??}
last2=${var#$firstPart}
print $firstPart $last2
done < yourFile
Vibhor Kumar Agarwal
Esteemed Contributor

Re: can sed search for last two digits of line...

Curt,
Can you explain that a bit to me.

I haven't done that type of scripting.
A bit of guidance required.

Thanks
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor
Solution

Re: can sed search for last two digits of line...

You can try as,

# cat > testfile
2088872167,15 1 01 047
2088872168,15 1 01 123
2088872166,15 1 01 546
2088872164,15 1 01 048

# perl -pe 's/(..)$/ $1/g' testfile
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48

or

# sed 's/\(..\)$/ \1/g' testfile
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48

hth.
Easy to suggest when don't know about the problem!
Ratzie
Super Advisor

Re: can sed search for last two digits of line...

That last one was exactly what I was looking for!

Thanks!