Print specific entries in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print specific entries in a file
# 1  
Old 05-08-2012
Print specific entries in a file

Guys,
I need your advice. I am attempting to print 1-n lines and then n-z lines.

If i enter in number it works fine e.g output lines 1 - 10 and 11 - 20, then it works fine.. But it fails when i pass string, as it outputs all the lines and not what i am asking for.

Code:
CAMHOSTS=`cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | wc -l`
export CAM_HALF=`expr $CAMHOSTS / 2`
echo "$CAM_HALF"

cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n '1,/$CAM_HALF/p'

NextLine=`expr $CAM_HALF + 1`

echo "$NextLine"
LAST=`echo "9999"`
cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n '/$NextLine/,/$LAST/p'


Output of the scripts

Code:
+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ wc -l
+ CAMHOSTS=261
+ expr 261 / 2
+ CAM_HALF=130
+ export CAM_HALF
+ echo 130
130
+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ sed -n '1,/$CAM_HALF/p'
 ipe-genp4n-nfs_13241231 LDN_UX_PRD
 ln7d5267apx_1323444 LDN_UX_DEV


+ expr 130 + 1
+ NextLine=131
+ echo 131
131
+ echo 9999
+ LAST=9999
+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ sed -n '/$NextLine/,/$LAST/p'

No output

# 2  
Old 05-08-2012
Environment Variable substitution does not work between single quote characters. Your sed statements are in single quote characters. Try double quote characters.

Also in the sed statments I think that you need a single space character before the p .
# 3  
Old 05-08-2012
I have enetered quotes and that looked to work...

But i have noticed that the oupur does not give a 50%/50% output.

More a 60%/50% ouput

261 Lines

Displays 146 lines first
Then 73 Lines next

Code:
cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n "1,/$CAM_HALF/ p "

How can i ensure i get 50%/50% ratio
# 4  
Old 05-08-2012
Quote:
Originally Posted by Junes
How can i ensure i get 50%/50% ratio
Code:
[root@x60 test]# a=$[`wc -l text | cut -f1 -d' '`] ; b=$[$a/2] ; echo "Lines: $a ; 1/2: $b" ; echo -n "first part: " ; head -$b text | wc -l ; echo -n "second part: " ; tail -$[$a-$b] text | wc -l
Lines: 33783 ; 1/2: 16891
first part: 16891
second part: 16892

?
# 5  
Old 05-08-2012
Your original code is written as if each record in the input file starts with a field containing the line number. If this is not so, then your sed commands need attention.
# 6  
Old 05-15-2012
USED awk to get around the issue,

cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i WAT |awk "NR>=$NextLine&&NR<=$WATHOSTS"

Last edited by Junes; 05-15-2012 at 05:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for specific key in a file and print it

Hi All, I have a file abc.txt Database unique name: NEPAL Database name: NEPAL Services: NEPAL_COB,NEPAL_PROD output i need is it should search "Services" and it that search "COB" word so output i need is like NEPAL_COB (3 Replies)
Discussion started by: amar1208
3 Replies

2. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

3. Shell Programming and Scripting

[Solved] How to print specific text from a file?

Hi All, I have the below text file from which I have to cut particular section starting from PTR_Security_Rpeorting.cpf to PTR_Security_Reporting_Env93_export. Report Model............: "D:\Cognos_Publishing\tmp.a2R94KLQec"\PTR_Security_Reporting.cpf Report Output Script....:... (4 Replies)
Discussion started by: Vikram_Tanwar12
4 Replies

4. Shell Programming and Scripting

Help to just print out specific line from an input file

Hi, I have a file which contains 2,500,500,432 lines. Can I know what command I should type in order just print out particular line from the input file? eg. I just wanna to see what is the contents at line 522,484,612. Thanks for advice. (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as (7 Replies)
Discussion started by: manigrover
7 Replies

6. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

7. UNIX for Dummies Questions & Answers

How to print the specific word in a file.

Hi , My input file is below like that :- $cat abc.txt Service name: test_taf Service is enabled Server pool: test_tac Cardinality: 2 Disconnect: false Service role: PRIMARY Management policy: AUTOMATIC DTP transaction: false AQ HA notifications: true Failover type: SESSION... (3 Replies)
Discussion started by: sp001
3 Replies

8. Shell Programming and Scripting

want to print the file content from the specific line

Hi All, I would like to print the content from the specific line of a file . For example... i have file abc.txt which has 100 lines of code ,from this file i would like to print the content from 20,19,18th line......like that Regards Srikanth (4 Replies)
Discussion started by: srikanthg
4 Replies

9. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

10. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies
Login or Register to Ask a Question