Delete characters from each line until meet character ":"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete characters from each line until meet character ":"
# 1  
Old 05-17-2012
Delete characters from each line until meet character ":"

Hello,

I have file that looks like this :

Code:
765327564:line1
94:line2
7865:line3
.....
765322:linen

I want to cut all the digits from the beginning of each line up to ":" character and to have everything like this :

Code:
line1
line2
line3
.....
linen

P.S : content of line1 ... linen may contain ":" character so to eliminate what I don;t want from each line with echo $line | cut -d ":" -f 2 will not work well.


any ideas?

THX.

Last edited by Scrutinizer; 05-17-2012 at 12:25 PM.. Reason: code tags
# 2  
Old 05-17-2012
you can use nawk , awk , gawk:


Code:
nawk -F":" '{print $NF}' infile > outfile

This User Gave Thanks to ahmad.diab For This Post:
# 3  
Old 05-17-2012
Quote:
Originally Posted by ahmad.diab
you can use nawk , awk , gawk:


Code:
nawk -F":" '{print $NF}' infile > outfile


Yes, thanks, this is what I was looking for.

---------- Post updated at 09:31 AM ---------- Previous update was at 09:29 AM ----------

One more question : do you know how to close a thread on this forum ?
# 4  
Old 05-17-2012
Code:
sed 's/.*://' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-17-2012
sorry , but I didn't see your important note that the lines may contain another ":" , my first code will not work if this is true ,kindly use below python code:


Code:
import re

fin = open("infile.txt",'r')
fout = open("outfile.txt",'w')
for line in fin:
     print(re.subn(r".*?:","",line,count=1)[0] , file=fout , end="")

fin.close()
fout.close()

BR

---------- Post updated at 17:47 ---------- Previous update was at 17:42 ----------

Quote:
Originally Posted by Scrutinizer
Code:
sed 's/.*://' infile

this will not work for below:

using bash:

Code:
$ echo "765327564:line1 hdvir:kvoir" | sed 's/.*://'
kvoir

# 6  
Old 05-17-2012
I know, but the OP was happy with the previous solution that did just that and so I thought that is what he meant. Otherwise he could use this:
Code:
sed 's/[^:]*://' infile

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-17-2012
Quote:
Originally Posted by Scrutinizer
I know, but the OP was happy with the previous solution that did just that and so I thought that is what he meant. Otherwise he could use this:
Code:
sed 's/[^:]*://' infile


Yes, NOW it works.

THX!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete a "G" character from df -h result

Hello everybody, I'm a new user in forum, In a script, i would like that the result sort : /usr - 665 Go (free: 631 Go / 6% used) but i obtain : /usr - 665G Go (free: 631G Go / 6% used) My command line is : RES_DF=`df -BG /usr/ | tail -n1 | awk -vOFS='' '{print $5, " - ", $1, "... (5 Replies)
Discussion started by: xnyp
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Find "*.c" and "Makefile" and then delete them with one line

find "*.c" and "Makefile" and then delete them with one line (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

deleting newline characters but not the "true" \n character

hi, i have a file that has about 4500 rows. this was an old microsoft access databse and what i am trying to do is take out the old extra \n newline characters but not take out the "true" newline character. I will explain. i was trying to write a regular expression, but that was not... (1 Reply)
Discussion started by: caddyjoe77
1 Replies
Login or Register to Ask a Question