Linux Command (Like Cat) to Read a Specified Quantity of Characters

Linux command (like cat) to read a specified quantity of characters

head works too:

head -c 100 file  # returns the first 100 bytes in the file

..will extract the first 100 bytes and return them.

What's nice about using head for this is that the syntax for tail matches:

tail -c 100 file  # returns the last 100 bytes in the file

You can combine these to get ranges of bytes. For example, to get the second 100 bytes from a file, read the first 200 with head and use tail to get the last 100:

head -c 200 file | tail -c 100

Is there a linux command (along the lines of cat or dd) that will allow me to specify the offset of the read syscall?

I figured out the issue I was having. For posterity, I will record the answer rather than just delete my question, because the answer wasn't necessarily easy to find.

Essentially, the issue occurred within FUSE. FUSE defaults to not using direct I/O (which is definitely the correct default to have, don't get me wrong), which is what resulted in the reads in size chunks of 4096 (these are the result of FUSE using a page cache of file contents [AKA a file content cache] in the kernel). For what I wanted to test (as explained in the question), I needed to enable direct I/O. There are a few ways of doing this, but the simplest way for me to do this was to pass -o direct_io as a command line argument. This worked for me because I was using the fuse_main call in the main function of my program.

So my main function looked like this:

int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &my_operations_structure, NULL);
}

and I was able to call my program like this (I used the -d option in addtion to the -o direct_io option in order to display the syscalls that FUSE was processing and the output/debug info from my program):

./prog_name -d -o direct_io test_directory

Then, I tested my program with the following simple test program (I know I don't do very much error checking, but this program is only for some quick and dirty tests):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
FILE * file;
char buf[4096];

int fd;

memset(&buf[0], 0, sizeof(buf));

if (argc != 4)
{
printf("usage: ./readTest [size] [offset] [filename]\n");
return 0;
}

file = fopen(argv[3], "r");
if (file == NULL)
{
printf("Couldn't open file\n");
return -1;
}

fd = fileno(file);

pread(fd, (void *) buf, atoi(argv[1]), (off_t) atoi(argv[2]));

printf("%s\n", buf);

return 0;
}

Counting number of characters in a file through shell script

This will do it for counting bytes in file:

wc -c filename

If you want only the count without the filename being repeated in the output:

wc -c < filename

This will count characters in multibyte files (Unicode etc.):

wc -m filename

(as shown in Sébastien's answer).

How to print the number of characters in each line of a text file

Use Awk.

awk '{ print length }' abc.txt

Count occurrences of a char in plain text file

How about this:

fgrep -o f <file> | wc -l

Note: Besides much easier to remember/duplicate and customize, this is about three times (sorry, edit! botched the first test) faster than Vereb's answer.

Read file from linux os using cat and ignore newline characters

It can be done using tr:

echo -n "Example Text" | tr -d "\n" > tmp_file

Or sed:

echo -n "Example Text" | sed -z "s/\n//g" > tmp_file

And if you want to remove only the newlines at the end of the input, it can be done like this:

echo -e "Example Text\nSome more Text\n\n" | sed -ze "s/[\n]*$//g" > tmp_file

The output will be

Example Text
Some more Text (without newline here)

Remove command or/and other character from a text column in linux

I'm not sure if it's the easiest way.

$ a="sender | recipient | this is a , subject | qty"
$ var1=`echo $a | cut -d'|' -f3 `
$ var2=`echo $a | cut -d'|' -f3 | tr -d ','`
$ echo $a | sed "s/$var1/$var2/"
  • after second line, var1 is the third column content
  • after third line, var2 is the third column content without ','
    because we removed it with tr command
  • finally replace var1 with var2

Count occurrences of a char in a string using Bash

I would use the following awk command:

string="text,text,text,text"
char=","
awk -F"${char}" '{print NF-1}' <<< "${string}"

I'm splitting the string by $char and print the number of resulting fields minus 1.

If your shell does not support the <<< operator, use echo:

echo "${string}" | awk -F"${char}" '{print NF-1}'


Related Topics



Leave a reply



Submit