Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a file containing some data as in the following example:

data1: it is my data
data2: some more data
data3: even some more data

What I want is the following output in another file:

it is my data
some more data
even some more data

Please guide me how to do it!

share|improve this question
    
should be migrated to unix.stackexchange.com –  i08in Mar 9 at 10:23
    
@Jobin why it should be migrated to unix.stackexchange.com? –  Avinash Raj Mar 9 at 12:14
    
@AvinashRaj: This is not specific to Ubuntu. –  i08in Mar 9 at 12:15
    
you didn't see any bash scripting commands before in AU? –  Avinash Raj Mar 9 at 12:17

5 Answers 5

up vote 8 down vote accepted

It's very simple if you use awk:

awk -F ': ' '{for (i=2; i<=NF; i++) print $i}' file_name > new_file_name

Each line is separated in more fields using ': ' as separator and print everything except the first field. The output is redirected to the new_file_name.

share|improve this answer
    
@Aditya he set the delimeter as : and awk considers the words before : as column 1 and the remaining part as column 2.After that the words from the column 2 are writed inside new_file. –  Avinash Raj Mar 8 at 17:38
    
@Radu am i correct? –  Avinash Raj Mar 8 at 17:40
    
@AvinashRaj You are very correct, thank you :) –  Radu Rădeanu Mar 8 at 17:47
3  
@Aditya note that this will not work if your lines can contain more than one :. If that is possible use my solution below. –  terdon Mar 8 at 17:50
    
@terdon Now will work ;) –  Radu Rădeanu Mar 8 at 17:54

This should work:

sed -i .bak 's/[^:]*: *//' file

Explanation: The -i .bak will edit the file in place, and create a backup of the original called filename.bak. s/pat/replacement/ means substitute pat with replacement. [^:]*: * means match the longest string of non-: characters, followed by a : then one or more space. The final result is that it will delete everything up to the first : and following spaces.

This approach has the advantage of working with data: or foo: or whatever and will also work if you have multiple : on the same line. For example, it can deal with this:

data: a line that contains : a colon!
share|improve this answer

This looks like a job for cut

cut -d: -f2- file > new_file
share|improve this answer

Open the file in vim editor as follows:

vi <filename>

and then press esc and then the following:

:%s/data.: //gc

Here %s is used for string replace

/ is the delimiter between the command and the strings

data.: is the first string, . is used for any single character gc is used to find and confirm before replacing

share|improve this answer
    
please tell me a link where I could learn stuff about vi editors since it looks very promising. –  khan Mar 9 at 9:56
    
@khan: Google is the best thing I could think of. May be you could start off here. –  i08in Mar 9 at 10:08

Your input file looks like an output of a grep command, say:

grep data data*

If this is really the case, you can use the -h option of grep command itself to get what you need:

grep -h data data*
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.