Create a new file from another file with lines begins with a specific string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a new file from another file with lines begins with a specific string
# 1  
Old 09-01-2014
Create a new file from another file with lines begins with a specific string

Hi,

I have a file with below format
myfile.txt
Code:
aa
aa1
aa2
qq
aa4
ghs
aa6
bbc
gdf

I m looking to create a file where lines begins with aa.

myfile1.txt
Code:
aa
aa1
aa2
aa4
aa6

Regards,
Litu

Last edited by Franklin52; 09-01-2014 at 11:30 AM.. Reason: Please use code tags
# 2  
Old 09-01-2014
What have yuo tried so far?
# 3  
Old 09-01-2014
Code:
cat /myfile.txt | awk '$1 ~ /^ aa*/' > /mynew.txt

---------- Post updated at 08:17 PM ---------- Previous update was at 08:13 PM ----------

Code:
awk '/aa/' /myfile.txt > newfile.txt

this seems to work. However i am looking for something without use of awk command.

Last edited by Scrutinizer; 09-01-2014 at 11:48 AM.. Reason: code tags
# 4  
Old 09-01-2014
Hello Litu,

awk can read the file so there is no need to add cat command there. You have got UUCA award Smilie Useless Use of Cat Award

Also you can remove starting space in following /^aa/ then you will get the correct output.


EDIT: Changed aa* to aa in suggestion. grep command can help us here for same.

Code:
grep "^aa" filename

Output will be as follows.

Code:
aa
aa1
aa2
aa4
aa6


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-01-2014 at 11:55 AM.. Reason: Adding one more solution to same, corrected awk solution
# 5  
Old 09-01-2014
Try:
Code:
grep '^aa' file > newfile

----
Quote:
Originally Posted by RavinderSingh13
[..]Also you can remove starting space in following /^aa*/ [][..]
That should be
Code:
/^aa/

This User Gave Thanks to Scrutinizer For This Post:
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 compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

Issue deleting all lines (having a specific string) in a file

I'm trying to create a script. There are 2 files - fileA.log & fileB.log fileA.log has the below data : aaaa cccc eeee fileB.log has the below data : cjahdskjah aaaa xyz jhaskjdhas bbbb abc ajdhjkh cccc abc cjahdskjah ... (7 Replies)
Discussion started by: Pandee
7 Replies

3. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

4. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

5. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

6. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

9. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

10. Programming

How to read specific lines in a bulk file using C file Programming

Please Help me I have a Bulk file containing Hex data I want to read specific lines from that bulk file by ID number. example ID DATE Time data 14 2005/09/28 07:40:08.546 0 5 078B1C 01916C 0FE59C 004B54 0A9670 0D04ED 05B6B4 0E2223... (10 Replies)
Discussion started by: rajan_ka1
10 Replies
Login or Register to Ask a Question