basic text manipulation in unix

Hi

I am an infrequent linux user. I have a text file with columns of data separated by colons

How do i change the column separator from a colon to a tab
How do i remove lines x-y of the file (e.g. lines 10-20)
How can i cut certain columns from the file (e.g. columns 2 and 5)
How do i rearrange the order of the remaining columns

I would appreciate really basic commands to do this and not necessarily the smartest way to do this involving 2 lines of code which I won't understand :)

thank you
andiejeAsked:
Who is Participating?
 
wesly_chenCommented:
> How do i change the column separator from a colon to a tab
perl -i -pe "s/\:/\t/g" /path-to-file

> How do i remove lines x-y of the file (e.g. lines 10-20)
sed 10,20d /path-to-file

> ow can i cut certain columns from the file (e.g. columns 2 and 5)
sed 2d file
sed 5d file
0
 
wesly_chenCommented:
> How do i rearrange the order of the remaining columns
Please give more details about this.
0
 
wesly_chenCommented:
Woo, mistake with column with row
> How can i cut certain columns from the file (e.g. columns 2 and 5)
cut -d ":" -f1,3-4,6-   file
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
wesly_chenCommented:
> How do i rearrange the order of the remaining columns
Usually "sort" command
Say, you want to sort by column 5,
$ sort -k5,5 /path-to-file
0
 
andiejeAuthor Commented:
regarding the order of columns, i don't want to sort the columns

if the original file has columns in the order c1 c2 c3

i want the new file with columns in the order c2 c3 c1 (this is an arbitary example)

thanks for your help
0
 
wesly_chenCommented:
awk '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}  /path-to-file
0
 
wesly_chenCommented:
woo, missing a single quote
awk '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}'  /path-to-file
0
 
andiejeAuthor Commented:
thanks for your replies. i will look at these later to make sure i can get them all to work :) and then close the question.
0
 
wesly_chenCommented:
For
cut -d ":" -f1,3-4,6-   file
it assumes each column separated by ":", if it separates by a blank space, then
cut -d " " -f1,3-4,6-   file

For
awk '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}'  /path-to-file
is assumes each column separated by blank space (inclue tab), if it separates by ":", then
awk -F":" '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}'  /path-to-file
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.