?
Solved

basic text manipulation in unix

Posted on 2011-04-21
9
Medium Priority
?
295 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:andieje
  • 7
  • 2
9 Comments
 
LVL 38

Accepted Solution

by:
wesly_chen earned 2000 total points
ID: 35445196
> 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
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35445207
> How do i rearrange the order of the remaining columns
Please give more details about this.
0
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 2000 total points
ID: 35445235
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Expert Comment

by:wesly_chen
ID: 35445250
> 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
 

Author Comment

by:andieje
ID: 35463134
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
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35463360
awk '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}  /path-to-file
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35463365
woo, missing a single quote
awk '{ temp  = $1; $1 = $2; $2 = $3; $3 = temp; print}'  /path-to-file
0
 

Author Comment

by:andieje
ID: 35468002
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
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 2000 total points
ID: 35468905
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question