To display the selected part in text file of unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To display the selected part in text file of unix
# 1  
Old 04-18-2012
To display the selected part in text file of unix

Code:
0400903071220312  20120322 20:21
1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363          12122011AUS          182644             000C2
8122011        0000                     000
1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363          12122011AUS          182644             000C2
8122011        0000                     0000803201205259005         -182644

I have the text file as above. I want to display '3071' from first line. Please assist with code. Help me out.Smilie

Last edited by Franklin52; 04-19-2012 at 03:46 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 04-18-2012
Code:
awk '{print substr($0, 7, 4); exit}' file

# 3  
Old 04-19-2012
Thanks

Thank you scott, the code works fine Smilie

---------- Post updated at 05:21 AM ---------- Previous update was at 05:04 AM ----------

Hi scott, In mentioned code what is $0 refers to ?
awk '{print substr($0, 7, 4); exit}' file
# 4  
Old 04-19-2012
$0 means the entire line
awk reads the file line by line
# 5  
Old 04-19-2012
Code:
sed 's/.\{6\}\(.\{4\}\).*/\1/g;q' infile

# 6  
Old 04-19-2012
awk reads the file line by line.ok. Does the 0 in $0 is refer to first line of the file ?
# 7  
Old 04-19-2012
Quote:
Originally Posted by rammm
awk reads the file line by line.ok. Does the 0 in $0 is refer to first line of the file ?
awk splits a text file into "records". by default this is a line (but can be changed).
each line (or record) is split into fields. by default this is done at spaces.

$0 then refers to the entire line at the moment. $1 is the first word, $2 the second, and so on, up to $NF (NF meaning number of fields)

The exit in the action block makes it quit before it ever gets to any other lines.
This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Dummies Questions & Answers

Hoe to copy selected strings from file into another text file

Hi Experts, I just want to copy some selected strings from a a file into a new .txt file . I am using below command to find the data now want to copy the search results into another .txt file please help me . find /Path -exec grep -w "filename1|filename1|filename1|" '{}' \;... (2 Replies)
Discussion started by: mumakhij
2 Replies

3. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

4. Shell Programming and Scripting

Need to display the output of ls -l selected columns

Hello Friends, Hope you are doing well. I am writing a shell script to find out the log file which are not updated in last 1 hours. I've almost completed the script but need your help in formatting its outputs. Currently, the output of the script is like this(as a flat row): ... (3 Replies)
Discussion started by: singh.chandan18
3 Replies

5. Shell Programming and Scripting

Add color to part of the text in a mail sent from unix script

Hi, We are using KSH. I was able to write a script where a mail is sent to the concerned persons and this is working perfectly file. I need to give a different color to a part of the data in the mail which. The script written is as follows; (echo "From: $REPLY" echo "To: $DLIST" echo... (6 Replies)
Discussion started by: jmathew99
6 Replies

6. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

7. Shell Programming and Scripting

display log at selected locations

Hello all I have a question on displaying log When i am redirecting the output to a log, it displays but in a format which i am not happy about. Can i give any explicit commands which says to display the above log at specific row and column I want to do this for each line of the log What is... (2 Replies)
Discussion started by: vasuarjula
2 Replies

8. UNIX for Advanced & Expert Users

display HTML text in body using unix mailX ????

display HTML text in body using unix mailX ????Hello, could any one tell me how to display text in html layout by sending a file using mailx command in unix. i know to use mailx : mailx -s "SUBJECT" user.name@domail.com < file_name.txt instead of txt file i want to send html page and... (8 Replies)
Discussion started by: sparan_peddu
8 Replies

9. UNIX for Dummies Questions & Answers

display full unix path as part of the command line

Hi all, Does anyone know how to ammend the .cshrc file in $HOME for your session to display the path as part of the command line? So that I dont need to keep on typing pwd to see where I am? thanks Ocelot (3 Replies)
Discussion started by: ocelot
3 Replies
Login or Register to Ask a Question