Extract File line and manipulate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract File line and manipulate
# 1  
Old 12-03-2009
Extract File line and manipulate

How can I print a section of each line in a text file. Eg

CODE1 XYR Test2 10319389
CODE2 XYR Test2 10319389
CODE3 XYR Test2 10319389
CODE4 XYR Test2 10319389
CODE5 XYR Test2 10319389

First thing that would be nice would a new file like, awk sed and substring may help but can't figure it out.

CODE1 XYR Test2
CODE2 XYR Test2
CODE3 XYR Test2
CODE4 XYR Test2
CODE5 XYR Test2

Second thing is would it be possible to create a new file but emiting certain lines based on start position and character length.
So I may want to remove string which start at line position 7 and are of length 3 character then I would get a new file

CODE1 Test2 10319389
CODE2 Test2 10319389
CODE3 Test2 10319389
CODE4 Test2 10319389
CODE5 Test2 10319389
# 2  
Old 12-03-2009
first case:

cut -f1-3 file_nm

second case:

need more info, like a sample input and sample output specifically.
# 3  
Old 12-03-2009
awk
Code:
$> awk '{ print $1" "$2" "$3 }' input.txt
CODE1 XYR Test2
CODE2 XYR Test2
CODE3 XYR Test2
CODE4 XYR Test2
CODE5 XYR Test2

$> awk '{ print $1" "$3" "$4 }' input.txt
CODE1 Test2 10319389
CODE2 Test2 10319389
CODE3 Test2 10319389
CODE4 Test2 10319389
CODE5 Test2 10319389

# 4  
Old 12-03-2009
Quote:
Originally Posted by frans
awk
Code:
$> awk '{ print $1" "$2" "$3 }' input.txt
CODE1 XYR Test2
CODE2 XYR Test2
CODE3 XYR Test2
CODE4 XYR Test2
CODE5 XYR Test2

$> awk '{ print $1" "$3" "$4 }' input.txt
CODE1 Test2 10319389
CODE2 Test2 10319389
CODE3 Test2 10319389
CODE4 Test2 10319389
CODE5 Test2 10319389

That's fine. OFS is space by default.

Code:
awk '{ print $1,$3,$4 }' input.txt

# 5  
Old 12-04-2009
Nice. Can you also do range so print columns 5 to 20.

---------- Post updated at 03:41 AM ---------- Previous update was at 03:14 AM ----------

Forgot that how do i print substring from each line and output

I tried but does not work:

awk '{print substr($1,1,5)}' | cat filename
# 6  
Old 12-04-2009
Quote:
Originally Posted by kelseyh
I tried but does not work:
awk '{print substr($1,1,5)}' | cat filename
You reverse the pipe : The pipe brings the output of previous command to the input of next command. so you should write
Code:
 cat filename | awk '{print substr($1,1,5)}'

but you can apply awk directly to the file like
Code:
awk '{print substr($1,1,5)}' filename

# 7  
Old 12-04-2009
bash shell
Code:
while read -r line
do
 set -- $line
 echo ${@:2:4}
done <"file"

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 manipulate string in line?

Hello, I looked up on google but do not know from which point to start... I am under ubuntu 18 bionic and Mainfile consists of 25K lines MainFile: Test,AAEE9FED3, GGBBDD DD AA X2d Moscow 112233445566aaBBccPPdddEE Test,AAEE9FED3, GG33DD s00022 Leningrad 11298932566aaBBccPPdddEE... (8 Replies)
Discussion started by: baris35
8 Replies

2. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Extract & Manipulate continous data stream-- tcpdump

Hello; I have this rather tricky problem to solve --(to me, anyways) .. I am processing the following one liner with tcpdump.. tcpdump -i T3501 -A ether host 00:1e:49:29:fc:c9 or ether host 00:1b:2b:86:ec:1b or ether host 00:21:1c:98:a4:08 and net 149.83.6.0/24 | grep --line-buffered -B... (5 Replies)
Discussion started by: delphys
5 Replies

4. Shell Programming and Scripting

Manipulate XML File Continous STRING by each Order Line using SHELL

heres sample File: <?xml version="1.0"?> <!DOCTYPE cXML SYSTEM "www"><cXML.............................................. <OrderRequest>USE UNIX.com</Extrinsic><Extrinsic name="UniqueName">Peter@UNIX.com</Extrinsic><Extrinsic name="ContractingEntity">UNIX... (3 Replies)
Discussion started by: Pete.kriya
3 Replies

5. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

6. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

10. UNIX for Dummies Questions & Answers

To manipulate a specific line

Hi, I would like to cut a specific line from a text file and then manipulate the text in that line. For eg. below is "tmp" file. ----------------- Tue 07/05/05 00:27:34.333 Tue 07/05/05 00:27:34.333 4 events were processed for customer 315 and will be correctly resolved when 315MERGE is run:... (2 Replies)
Discussion started by: dhiman.sarkar
2 Replies
Login or Register to Ask a Question