cut in sed/awk

Hi
Can i have an example where i should be able to cut columns
(like for eg cut -c 1-3) in sed or awk.

Regards
Dhana

Using Sed,

echo "Testing" | sed 's#^\(...\)\(.*\)#\1#g'

Output
Tes

Thanks
Penchal

Using awk:

awk '{print $1,$2,$3}' inputfile > outputfile

Hi Annihilannic,

awk '{print $1,$2,$3}' prints the first,second and third fileds of the current record.
But i think here the requirement is to print the first 3 characters of the record.
Please correct me if iam wrong

Thanks
Penchal

use substr with awk

examples : search from the forum :slight_smile:

there is some problem in the first thread itself

cut -c doesn't stand for columns ... characters

It will specify u character position

Ah, yes, so there is. No wonder I was confused. :wink:

HI All
THe requirement is cut the individual characters and not columns.
For example if i have the string "TESTING"
i want to get the output as STI i.e 3-5.

Regards
Dhana

HI

echo "Testing" | sed 's#^\(...\)\(.*\)#\1#g'
This one works.

If i have the following records in the file

TESTING
SSDFFFE
SDWERE
WEREERR
WEYUIER

If i want to cut the 3-5 characters , like the below
STI
DFF
WER
REE
YUI

is there any option that i need to include in sed ?

Regards
dhana

HI All
Please igmore my previous query. as i was able to do it.

ONe more question.
Do you have formatting in sed
like
printf '%s %-50s" in sed so that i want the outputted columns to print in a particular format

Regards
Dhana

Hi All
Any answers to my question in sed or awk ?

Regards
Dhana

Pls don't 'bump-up' posts - it's against the Rules.

It's also not clear which parts you have solved yourself already, as the second last message said "ignore my previous question". Maybe you should ask a more focused question, in a separate thread if it's not directly related to the original topic of this thread.

sed does not have print formatting modifiers like sprintf, although of course, if you are persistent, you can achieve the same effect using sed constructs (but I would not recommend it, unless you want to achieve sed guru status for its own sake).

sed 's/^..\(...\).*/\1/' will print chararacters 3 through 5 from each input line. Lines with fewer than five input characters will be printed unmodified; fixing this can be discussed if it's a requirement.