How to read specific lines from a file (by line number)?
If the file to read is big, and you don't want to read the whole file in memory at once:
fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
fp.close()
Note that i == n-1
for the n
th line.
In Python 2.6 or later:
with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
Quick unix command to display specific lines in the middle of a file?
with GNU-grep you could just say
grep --context=10 ...
How to read a specific line using the specific line number from a file in Java?
Unless you have previous knowledge about the lines in the file, there's no way to directly access the 32nd line without reading the 31 previous lines.
That's true for all languages and all modern file systems.
So effectively you'll simply read lines until you've found the 32nd one.
Read a line from file in C and extract the number of input
the loop while (sscanf(line, "%d ", &number))
keeps parsing the first number in the line.
You should use strtol
instead:
#include <stdio.h>
#include <stdlib.h>
#define STRING_SIZE 2000
int main() {
FILE *fp = fopen("in.dat", "r");
char line[STRING_SIZE];
int lcount = 0, nline = 1;
if (fp != NULL) {
while (fgets(line, STRING_SIZE, fp) != NULL) {
if (lcount == nline) {
char *p = line, *q;
int count = 0;
for (;;) {
long val = strtol(p, &q, 0); // parse an integer
if (q == p) {
// end of string or not a number
break;
}
// value was read into val. You can use it for whatever purpose
count++;
p = q;
}
printf("%d\n", count);
break;
} else {
lcount++;
}
}
fclose(fp);
}
return 0;
}
CentOS - displaying 1 line of file only
the correct command is:
sed -n ${number}p /etc/passwd
(p is command to print)
Of course sed
will do nothing useful after that, so it's even better to quit after having printed like Benjamin hinted to avoid reading the rest of the file:
sed -n ${number}'{p;q}' /etc/passwd
How to Read Certain Lines of A Data File Into R
Check this out:
con <- file("test1.txt", "r")
lines <- c()
while(TRUE) {
line = readLines(con, 1)
if(length(line) == 0) break
else if(grepl("^\\s*F{1}", line) && grepl("(0,0)", line, fixed = TRUE)) lines <- c(lines, line)
}
lines
# [1] "F 20160602 14:25:11.321 F7982D50 GET 156.145.15.85:37525 xqixh8sl AES \"/pcgc/public/Other/exome/fastq/PCGC0077248_HS_EX__1-06808__v3_FCC49HJACXX_L7_p1of1_P1.fastq.gz\" \"\" 3322771022 (0,0) \"1499.61 seconds (17.7 megabits/sec)\""
Pass the file stream to readLines
so that it can read it line by line. Use regular expression ^\\s*F{1}
to capture line starting with letter F
with possible white spaces where ^
denote the beginning of a string. Use fixed=T
to capture the exact match of (0,0)
. If both of the checks are TRUE
, append the result to lines.
Data:
D 20160602 14:15:43.559 F7982D62 Req Agr:131 Mra:0 Exp:0 Mxr:0 Mnr:0 Mxd:0 Mnd:0 Nro:0
D 20160602 14:15:43.559 F7982D62 Set Agr:130 Mra:0 Exp:0 Mxr:0 Mnr:0 Mxd:0 Mnd:0 Nro:0 I 20160602 14:15:43.559 F7982D62 GET 156.145.15.85:36773 xqixh8sl AES "/pcgc/public/Other/exome/fastq/PCGC0065109_HS_EX__1-04692__v3_FCAD2HMUACXX_L4_p1of1_P2.fastq.gz" ""
M 20160602 14:15:43.595 DOC1: F7982D62 Request for unencrypted meta data on encrypted transaction
M 20160602 14:15:48.353 DOC1: F7982D62 Transaction has been acknowledged at 722875647
F 20160602 14:15:48.398 F7982D62 GET 156.145.15.85:36773 xqixh8sl AES "/pcgc/public/Other/exome/fastq/PCGC0065109_HS_EX__1-04692__v3_FCAD2HMUACXX_L4_p1of1_P2.fastq.gz" "" 50725464 (4,32) "Remote Application: Session Aborted: Aborted by user interrupt"
M 20160602 14:15:48.780 DOC1: F7982D63 New download request D 20160602 14:15:48.780 F7982D63 META: 134 Path: /pcgc/public/CTD/exome/fastq/PCGC0033175_HS_EX__1-00304-01__v1_FCBC0RE4ACXX_L3_p32of96_P2.fastq.gz user: xqixh8sl pack: arg: feat: cE,s
F 20160602 14:25:11.321 F7982D50 GET 156.145.15.85:37525 xqixh8sl AES "/pcgc/public/Other/exome/fastq/PCGC0077248_HS_EX__1-06808__v3_FCC49HJACXX_L7_p1of1_P1.fastq.gz" "" 3322771022 (0,0) "1499.61 seconds (17.7 megabits/sec)"
How display specific line in HDFS file which contains special string
Similar to *nix, the -ls
option lists the files and directories in a given path, it doesn't read hdfs file content. See HDFS file system shell documentation.
The command you're probably looking for is -cat
:
$ hdfs dfs -cat /hdfs/data/t11/t111/t1/t/z/InterfacePublique/* | grep "DIS_CD_RLT_PSE.*I"
Open a file in Visual Studio at a specific line number
I can't figure out a way to do this with straight command-line options. It looks like you will have to write a macro for it. Supposedly, you can invoke them like so.
devenv /command "Macros.MyMacros.Module1.OpenFavoriteFiles"
So, you can probably create a macro that takes a filename and a line number, then opens the file and jumps to the proper place. But, I don't know that you can specify a same-instance flag somewhere, or not.
Related Topics
How to Close a Netcat Connection After a Certain Character Is Returned in the Response
How to Emulate the Raspberry Pi 2 on Qemu
How to Untar a Tar.Bz File in Unix
How to Get a List of All Valid Ip Addresses in a Local Network
Linux Usb: Turning the Power on and Off
Linux's Thread Local Storage Implementation
Postgresql -Bash: Psql: Command Not Found
Why Can't You Sleep While Holding Spinlock
Can You Enter X64 32-Bit "Long Compatibility Sub-Mode" Outside of Kernel Mode
Why Does %Rbp Point to Nothing
How to Install Packages from Command Line on Suse
Replacing Environment Variables in a Properties File
How to Run Dos2Unix on an Entire Directory
Randomly Pick Lines from a File Without Slurping It with Unix