5

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!

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

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!
8

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.

6
  • @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 '14 at 17:38
  • @Radu am i correct? – Avinash Raj Mar 8 '14 at 17:40
  • 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 '14 at 17:50
  • @terdon Now will work ;) – Radu Rădeanu Mar 8 '14 at 17:54
  • @terdon: I am not the OP. I had left the comment to explain because a command without any explanation looks dirty :-) – Aditya Mar 8 '14 at 17:56
4

This looks like a job for cut

cut -d: -f2- file > new_file
1

You can use Vim in Ex mode:

ex -sc '%s/[^:]*: *//|x' file
  1. % search all lines

  2. s substitute

  3. x save and close

0

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

2
  • please tell me a link where I could learn stuff about vi editors since it looks very promising. – Naseer Mar 9 '14 at 9:56
  • @khan: Google is the best thing I could think of. May be you could start off here. – jobin Mar 9 '14 at 10:08
0

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*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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