Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
Why can't add the header in the df's output?
firefox
and exclude grep
process,it is no use to show all processes here,omit many lines.
ps aux | grep [f]irefox
debian 7069 1.0 4.4 3134148 359168 ? Sl 11:58 0:12 /usr/lib/firefox-esr/firefox-esr
debian 7128 0.0 0.4 223884 36824 ? Sl 11:58 0:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20241118130310 -prefsLen 28341 -prefMapSize 249085 -appDir /usr/lib/firefox-esr/browser {0c853969-95e1-4db0-9e95-eeaee3d4f814} 7069 true socket
The output does not contain header info in ps
's command ,in order to get the header,add head -n1
after the pipe.
ps aux |(head -n 1 ;grep [f]irefox)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
debian 7069 0.9 4.4 3134148 361740 ? Sl 11:58 0:13 /usr/lib/firefox-esr/firefox-esr
debian 7128 0.0 0.4 223884 36824 ? Sl 11:58 0:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20241118130310 -prefsLen 28341 -prefMapSize 249085 -appDir /usr/lib/firefox-esr/browser {0c853969-95e1-4db0-9e95-eeaee3d4f814} 7069 true socket
Other bash command:
df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 3977796 0 3977796 0% /dev
tmpfs 804900 1356 803544 1% /run
/dev/sdb1 460349516 143209832 293681076 33% /
tmpfs 4024488 100444 3924044 3% /dev/shm
tmpfs 5120 16 5104 1% /run/lock
df | grep shm
tmpfs 4024488 101536 3922952 3% /dev/shm
df |(head -n1; grep shm)
Filesystem 1K-blocks Used Available Use% Mounted on
Why can't get the below output when to execute df |(head -n1; grep shm)
?
df |(head -n1; grep shm)
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 4024488 101536 3922952 3% /dev/shm
Why grep in "ps aux |(head -n 1 ;grep [f]irefox)" can get lines to match?
As expert user10489
point out:
The command df |(head -n1; grep shm) does this:
df generates some output
head takes all of the output, prints the first line, and then quits throwing away all the rest of what it read.
There is no output left for grep to take as input.
Another post can explore it in depth:
cat > raw.txt <,if head -2;
run as user10489
say:head takes all of the output, prints the first line, and then quits throwing away all the rest of what it read.
,why sort -n
have lines to sort with?
The result would be
ID DESCRIPTION
----- --------------
No lines sorted!!!
Head function behaves differently inside an sh script and in terminal. Why?
x2
having
08PKj00000YdniC
09:59:04.53 (130409269)|SYSTEM_METHOD_EXIT||System.debug(ANY)
... many other lines from logs below
I am trying to take the first line and discard everything else.
When I execute
echo $x2 | head -n 1
in terminal,
I receive correct output
08PKj00000YdlM5
However, when I have an sh script and execute it from terminal like
./unwrap2.sh verbose
I see strange result
08PKj00000YdniC 09:59:04.53 (130409269)|SYSTEM_METHOD_EXIT||System.debug(ANY) ... many other lines from logs below
which looks like those lines are joined together even though I didn't expect this.
What am I doing wrong inside a script and how can I fix this?
Listing of unwrap2.sh
shell script
verbose=$1
# deploy data cloud Data Kit
sf project deploy start -d dc
# unwrap Data Kit components
x=$(sf apex run -f scripts/unwrap.apex --json | jq '.result.logs' -r)
x1=${x#*EXECUTION_STARTED}
x2=${x1#*Result is \(success): }
if [[ "$verbose" = "verbose" ]]; then
echo "x2: $x2"
fi
x3=$(echo $x2 | head -n 1)
if [[ "$verbose" = "verbose" ]]; then
echo "x3: $x3"
fi
status=$(sf data query -q "select Id,Status FROM BackgroundOperation WHERE Id = '$x3'" --json | jq '.result.records.Status' -r)
if [[ "$verbose" = "verbose" ]]; then
echo "select Id,Status FROM BackgroundOperation WHERE Id = '$x3'"
fi
echo "Status: $status"
while [[ "$status" != "Complete" ]]; do
sleep 10
status=$(sf data query -q "select Id,Status FROM BackgroundOperation WHERE Id = '$x3'" --json | jq '.result.records.Status' -r)
echo "Status: $status"
done
When I execute
x3=$(echo $x2 | head -n 1)
in terminal, it works fine.
Printing only the file name after searching a pattern in the first line (using grep and head)
#!/bin/bash
on the first line. So far I tried this
head -n 1 $filename | grep -l "pattern"
but when I execute it instead of the name of the files I receive
(standard input)
Like I said, I'm a beginner and so far I'm familiar with simple commands. So I would like to know where I f****** up and if there is a way of acheiving what I want without using harder commands like awk
.
for fis in find -perm -a+x -name "*.sh" -type f
do
head -n 1 $fis | grep -l "#!/bin/bash"
done
How to move all files whose contents begin with 0?
head -c 1 filename
.
Extract parts of a string using head and tail only
SHOWpijfirefjTHISezpijSTRING
, the command should output 'SHOWTHISSTRING'
I tried something like
(head -c 4 mdp > /dev/tty) | (tail -c +13 mdp | head -c 4 > /dev/tty) | (tail -c 6 mdp > /dev/tty) 2>&1
but it doesn't always return the same result and can give out of order results
Head/Tail command to grab multiple sets of lines
Head and tail of files in a directory, selected by the user
~/bin/new/*
).
This script works, but I would like to allow the user to not only show the head or the tail but so that they can select the directory.
The current script is written as:
while true;do
echo "Would you like to list the head or tail?"
read headortail
if [[ $headortail = head ]]; then
head -n 4 ~/bin/new/*
break
elif [[ $headortail = tail ]]; then
tail -n 4 ~/bin/new/*
break
else
echo "Invalid Input - Please Input head or tail"
fi
done
If I add another line under the first echo
such as:
echo "Please type the directory"
read dirLocation
How could I implement this to the script? Would I do:
head -n 4 $dirLocation
Would that work?
head not returning n lines
head -n 445 /etc/snort/snort.conf | nl
I expect lines 1-445 to be returned. However, only up to line 371 is returned:
[snip]
370 preprocessor dcerpc2_server: default, policy WinXP, \
371 detect [smb [139, 445], tcp 35, udp 135, rpc-over-http-server 593], \
What is happening?
cat line X to line Y on a huge file
cat
the lines X
to Y
(e.g. 57890000 to 57890010).
From what I understand I can do this by piping head
into tail
or viceversa, i.e.
head -A /path/to/file | tail -B
or alternatively
tail -C /path/to/file | head -D
where A
,B
,C
and D
can be computed from the number of lines in the file, X
and Y
.
But there are two problems with this approach:
1. You have to compute A
,B
,C
and D
.
2. The commands could pipe
to each other **many more** lines than I am interested in reading (e.g. if I am reading just a few lines in the middle of a huge file)
Is there a way to have the shell just work with and output the lines I want? (while providing only X
and Y
)?
Can `head` read/consume more input lines than it outputs?
$ { head -n 1; cat; } < <(printf 'a\nb\nc\n')
a
What is causing the different output from those scripts?
---
Additional info - this is apparently not just a head
problem:
$ printf 'a\nb\nc\n' | { sed '1q'; cat; }
a
$ printf 'a\nb\nc\n' | { awk '1;{exit}'; cat; }
a
$ { sed '1q'; cat; } < <(printf 'a\nb\nc\n')
a
$ { awk '1;{exit}'; cat; } < <(printf 'a\nb\nc\n')
a
---
What would be a robust, POSIX way in shell (i.e. without just invoking awk or similar once to do everything) to read some number of lines from input and leave the rest for a different command regardless of whether the input is coming from a pipe or a file?
---
This question was inspired by comments under an answer to [_sort the whole .csv based on the value in a certain column_](/a/750386/133219).
limit find output AND avoid signal 13
find
continues to work. This is well explained [here](https://superuser.com/a/554896/317607) . I tried quit
action:
find /path/ -exec grep -H -m 1 'pattern' \{\} \; -quit
This outputs only the first match.
Is it possible to limit find output with specific number of results (like providing an argument to quit
similar to head -n
)?
How to do `head` and `tail` on null-delimited input in bash?
find
command can output names of files as a null-delimited strings (if -print0
is provided), and xargs
can consume them with -0
option turned on. But in between, it's hard to manipulate that collection of files - sort
command has -z
switch, that makes it possible to sort those files, but head
and tail
don't have them.
How can I do head
and tail
on those null-delimited inputs in a convenient way? (I can always create a short & slow ruby script, but I hope that there could be a better way)
Only pipe output if at least n lines
tail
but instead of only returning n lines, it would return *all* lines but only if there are at least n lines (the header).
A colleague wrote a small Python tool to do this but it needs installation on all machines where I need to do this.
Is there a short command line that can achieve this effect using a standard tool (maybe awk?) ?
Rounding off negative decimal and positive decimal number
How to obtain inverse behavior for `tail` and `head`?
head
/tail
a document and get the reverse output; because you don't know how many lines there are in a document?
I.e. I just want to get everything but the first 2 lines of foo.txt
to append to another document.
Negative arguments to head / tail
M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
**last** lines **and** the **first** N
lines of a given file?
Creating a script which compares the return value from an AT command
connected=$(echo -e 'AT+CREG?\r' > /dev/ttyUSB2 | head -2 /dev/ttyUSB2)
echo "\n"
compare=" $\'\r\' +CREG: $\'0,1\r"
echo "\n"
echo ${connected}
echo "\n"
if [ ${connected} = "${compare}" ] ; then
echo "This may not be an AT&T card"
else
echo "Missed the if statement"
fi
Display line number in head and tail command like `cat -n`
cat
without -n
**
user@linux:~$ cat /etc/sysctl.conf | head -4
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
user@linux:~$
There is -n
option in cat
command to display the line number of the file.
user@linux:~$ cat -n /etc/sysctl.conf | head -4
1 #
2 # /etc/sysctl.conf - Configuration file for setting system variables
3 # See /etc/sysctl.d/ for additional system variables.
4 # See sysctl.conf (5) for information.
user@linux:~$
user@linux:~$ cat -n /etc/sysctl.conf | tail -4
74 # Debian kernels have both set to 1 (restricted)
75 # See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
76 #fs.protected_hardlinks=0
77 #fs.protected_symlinks=0
user@linux:~$
What about head
and tail
command? Is there any option to view the line number in head
and tail
without using cat -n
and pipe the output to head
or tail
?
I've tried head -n
and tail -n
but no line number displayed on the output.
user@linux:~$ head -n4 /etc/sysctl.conf
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
user@linux:~$
user@linux:~$ tail -n4 /etc/sysctl.conf
# Debian kernels have both set to 1 (restricted)
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0
user@linux:~$
broken pipe error with popen and JS ffi
head
would take what it needs and tr
would just read forever. At least it should throw broken pipe; head
would consume the first 8 bytes and then tr
would still be putting out output and broken pipe would be thrown by tr
because head
has stopped running.
Both situations make sense to me, but it seems that they are some what exclusive to each other. I don't understand what is different between calling:
exec(tr -dc "[:alpha:]" < /dev/urandom | head -c ${1-8})
and
tr -dc "[:alpha:]" < /dev/urandom | head -c ${1-8}
directly from the command line, and specifically why <
an endless file into something and then |
it to something makes it not run endlessly. I've been doing this for years and never questioned why it works this way.
Lastly, is it OK to ignore this broken pipe error? Is there a way to fix it? Am I doing something wrong in my C++ ish javascript code? Am I missing some kind of popen basics?
------ EDIT
messing around some more the code
exec('head -10 /dev/urandom | tr -dc "[:alpha:]" | head -c 8')
throws no pipe error!