How do I remove everything after a certain character in text files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I remove everything after a certain character in text files?
# 1  
Old 01-14-2009
How do I remove everything after a certain character in text files?

I have a text file with a few thousand lines in the format:

Code:
abcdef*ghijk*lmno
pqrs*tuv
wx*y*z

etc.

What I want to do is, get rid of everything after the SECOND asterik (the *) in each line (including the asterik). So for the example above, it would look like this after the editing:

Code:
abcdef*ghijk
pqrs*tuv
wx*y

How can I do this?
# 2  
Old 01-14-2009
Code:
$ cut -f 1,2 -d'*' filename

# 3  
Old 01-14-2009
Quote:
Originally Posted by agn
Code:
$ cut -f 1,2 -d'*' filename

That didn't work; there are still lines with multiple asteriks in them Smilie
# 4  
Old 01-14-2009
Can you paste input lines where you get more than one '*' in output.
# 5  
Old 01-14-2009
Quote:
Originally Posted by agn
Can you paste input lines where you get more than one '*' in output.
Code:
foods: vegetables *baby carrots*cabbage*lettuce*tomatoes*

In the original file, I had about 5 more vegetables listed (for this example line).
# 6  
Old 01-14-2009
Code:
$ echo "foods: vegetables *baby carrots*cabbage*lettuce*tomatoes*" | cut -f 1,2 -d'*'
foods: vegetables *baby carrots

Where do you see multiple '*'s in the output ?
# 7  
Old 01-14-2009
Quote:
Originally Posted by agn
Code:
$ echo "foods: vegetables *baby carrots*cabbage*lettuce*tomatoes*" | cut -f 1,2 -d'*'
foods: vegetables *baby carrots

Where do you see multiple '*'s in the output ?
I just ran the command, it spit out everything, and I checked the file again, and everything was the same
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Windows & DOS: Issues & Discussions

Remove duplicate lines from text files.

So, I have text files, one "fail.txt" And one "color.txt" I now want to use a command line (DOS) to remove ANY line that is PRESENT IN BOTH from each text file. Afterwards there shall be no duplicate lines. (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

How to loop a text file to remove the old files?

Hi , I'm using Ksh 88 version. I've a text file which contains the directory names DIR1 DIR2 ---- DIR10 I've to remove the data which has more that 30 days from the above directories . The data has to be removed from the following path cd /home/etc/DIR1/hist cd /home/etc/DIR2/hist... (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Script to search for a character in files in a Directory & remove it

Hi All, Am new to both Unix & this Forum - Need some help on a script that I am trying to write: In a Directory i have few text files which might or might not contain some text that I am trying to find. Once that text is found in any of the files, it needs to be removed from the file ... (6 Replies)
Discussion started by: rituparna_gupta
6 Replies

5. Shell Programming and Scripting

How to remove common file names from text files

I'm running on freebsd -- with a default shell of csh. I have two files named A and B. Each line of each file contains a file name. How can I write a script that removes all the file names in file B from A. I tried to use perl to create a huge regular expression with "|" separating the file... (2 Replies)
Discussion started by: siegfried
2 Replies

6. Shell Programming and Scripting

how to remove all text including 2 certain character in each line!

Hi I have a file which has aroun 200 line and it is like this: GROUP2-WDI">GROUP2-WDI GROUP3-WDI">GROUP3-WDI KL2P0508BC">KL2P0508BC KL2P0508BIT">KL2P0508BIT KL3P0506BC">KL3P0506BC KL3P0506BUS">KL3P0506BUS KLD1F0507DBT">KLD1F0507DBT KLD1F0507DIT">KLD1F0507DIT KLD1F0510DBT">KLD1F0510DBT... (3 Replies)
Discussion started by: digitalmahdi
3 Replies

7. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

8. Shell Programming and Scripting

Remove duplicate files based on text string?

Hi I have been struggling with a script for removing duplicate messages from a shared mailbox. I would like to search for duplicate messages based on the “Message-ID” string within the messages files. I have managed to find the duplicate “Message-ID” strings and (if I would like) delete... (1 Reply)
Discussion started by: spangberg
1 Replies

9. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies

10. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies
Login or Register to Ask a Question