awk extract a string from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk extract a string from a file
# 1  
Old 03-18-2010
awk extract a string from a file

Hi,

I have a file which has thousand of lines with lines starting with

Quote:
[2010-03-03 16:28:44.035] DEBUG 2010-03-03 16:28:44,035 DEBUG :[ACTIVE]
And I want to extract and show to user only the below string from all the lines
Quote:
[2010-03-03 16:28:44.035]
Please note note that the above string is a time stamp and it would be different on all the lines.

Please tell me how to extract and show only these string from a file.

Thanks in advance
# 2  
Old 03-18-2010
If all that you are interested in is the time stamp , a simple
Code:
cat <file>|cut -c1-25

will do the job...
# 3  
Old 03-18-2010
Something like this?

Code:
 
sed 's/\(\[.*\]\) .*/\1/'  input_file

# 4  
Old 03-18-2010
Tools using awk

Code:
awk -F"]" '{print $1"]"}' <myfile.txt

or
Code:
awk '{print $1,$2}' <myfile.txt

# 5  
Old 03-18-2010
using awk 2

awk '{print $NR, $(NR+1)}'
# 6  
Old 03-18-2010
Quote:
Originally Posted by kvaikla
awk '{print $NR, $(NR+1)}'
What does that do exactly?
And how's it related to the OP?
# 7  
Old 03-18-2010
Quote:
Originally Posted by vgersh99
What does that do exactly?
And how's it related to the OP?
thanks to your question, I found a mistake. to use awk built-in variables it should be
Code:
awk '{print $(NF-6) " " $(NF-5)}'

For example
Code:
# cat list.txt
[2010-03-03 16:28:44.035] DEBUG 2010-03-03 16:28:44,035 DEBUG :[ACTIVE]
[2010-03-04 16:28:44.035] DEBUG 2010-03-03 16:28:44,035 DEBUG :[ACTIVE]
[2010-03-05 16:28:44.035] DEBUG 2010-03-03 16:28:44,035 DEBUG :[ACTIVE]
# awk '{print $(NF-6) " " $(NF-5)}' < list.txt
[2010-03-03 16:28:44.035]
[2010-03-04 16:28:44.035]
[2010-03-05 16:28:44.035]

NR works only if in input fail i only one row
thnx
Kaido

Last edited by Franklin52; 03-19-2010 at 05:28 AM.. Reason: Please use code tags, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

3. Shell Programming and Scripting

Extract all proper names from string with awk

I want to extract the proper names with awk from a very long string, like: õ(k): &lt;/span&gt;<br /><a something="pls/pe/person.person?i_pers_id=3694&amp;i_topic_id=2&amp;i_city_id=3372&amp;i_county_id=-1" target="_blank"><b>Gary Oldman</b></a> (George Smiley)<br /><a... (12 Replies)
Discussion started by: lyp
12 Replies

4. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

5. Shell Programming and Scripting

how to extract a paticular string from the text file with awk.

hello forum members I have txt file which consists the following information. Server: abababa.xyz.ap.mxmx.com Address: 111.143.211.202 Name: rmxd.ipc.ap.mxmx.com Address: 144.111.99.9 from the abovefile i have to extract only string "rmxd.ipc.ap.mxmx.com" through awk command.... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

6. Shell Programming and Scripting

Extract a string from file

Below are the content of my file and i need to extract the 6 digit numbers after the word barcode, how can i do this? for example i need to extract 004119,004275,004030 to a new file. Logically move media ID 004119 (barcode 004119) from standalone to slot 18. Logically move media ID 004275... (9 Replies)
Discussion started by: shehzad_m
9 Replies

7. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

8. Shell Programming and Scripting

Extract a string from a file

Input File: ===================================== "Server1" srvgrp="group1" srvid=10 CLOPT="-A -r -e /path/logfile -o /path/stdout" VAR1=0666 VAR2=N VAR3=0666 MIN=3 VAR4=4 VAR5=N VAR6=FASTPATH VAR7=5 VAR8=86400 VAR9=Y ... (20 Replies)
Discussion started by: kaustubh137
20 Replies

9. Shell Programming and Scripting

Using Awk in shell script to extract an index of a substring from a parent string

Hi All, I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this. Requirement : Need to get the index of a substring from a parent string Eg : index("Sandy","dy") should return 4 or 3. My Approach : I used Awk function index to... (2 Replies)
Discussion started by: sandeepms17
2 Replies

10. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies
Login or Register to Ask a Question