Operating System - HP-UX
1834349 Members
2038 Online
110066 Solutions
New Discussion

[Q] simple shell command question ..

 
Tony, Lim
Frequent Advisor

[Q] simple shell command question ..

I would like to split some characters that have a 9 digit, and same character - 1 "m", 8 numeric characters such as
$ cat test.txt
m12345678
m76543210
m09876543
m98765432
...

I would like to split this 9 digit to 8 digit.
(want to remove last character such as
m1234567
m7654321
...

Would you recommand any command to do this ?
like awk, sed etc ...

I am not sure what option is available to this at awk.

Thanks in advance.
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: [Q] simple shell command question ..

What about the cut command? Man cut for details.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: [Q] simple shell command question ..

Hi Tony:

TMTOWTDI (There's More Than One Way To Do It). Here's one using shell commands and 'cut':

while read LINE
do
echo ${LINE}|cut -c1-8
done < filein > fileout

Regards!

...JRF...
Luk Vandenbussche
Honored Contributor

Re: [Q] simple shell command question ..

cut -c1-8 test.txt > output.txt
James R. Ferguson
Acclaimed Contributor

Re: [Q] simple shell command question ..

Hi (again) Tony:

For fun, and brevity, you could do:

# perl -pli.old -e 'chop' file

This reads and chops the last character of every line (which is what you wanted); makes a backup copy of your input file as "file.old" and replaces "file" with the modified copy.

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: [Q] simple shell command question ..

Hi,

don't know what your statement about 'm' means - I guess only strings starting with 'm' and length 9 should be modified.

awk '$1 ~ /^m/ && length($1)==9 {print substr($1,1,8);next}
{print}' test.txt

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: [Q] simple shell command question ..

How about giving sed a try...

# sed '/^m/s/\(.*\).$/\1/g' test.txt

OR with awk as...

# awk '{sub(/.$/,"");print $0}' test.txt

cheers!
Sandman!
Honored Contributor

Re: [Q] simple shell command question ..

Update to my last post as I forgot to include the conditional evaluation that the string should start with an "m" and should have exactly nine characters.

sed way...

# sed -n '/^m.\{8\}$/ s/\(.*\).$/\1/p' test.txt

awk way...

# awk '$0~/^m/ && length($0)==9 {sub(/.$/,"");print $0}' test.txt

cheers!
Sandman!
Honored Contributor

Re: [Q] simple shell command question ..

Yet another oops! as the string should have exactly nine characters, starting with "m" and followed by eight numerics...so here's the final version:

sed version...

# sed -n '/^m[0-9]\{8\}$/ s/\(.*\).$/\1/p' test.txt

awk version...

# awk '$0~/^m[0-9]{8}$/ {sub(/.$/,"");print $0}' test.txt

hope it helps!!!

curt larson_1
Honored Contributor

Re: [Q] simple shell command question ..

just do it in the shell:

while read var restOfLine
do
print "${var%?}"
done < yourFile