Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
-3
votes
5
answers
69
views
Perl or sed script to remove a specific content of a file from another bigger file
I have a file **(FILE1)** with some repeated sections as below (example): LINE 1 ABCD LINE 2 EFGA LINE 3 HCJK REMOVE LINE11 REMOVE LINE12 REMOVE LINE13 LINE 4 ABCDH LINE 5 EFGAG LINE 6 HCJKD REMOVE LINE11 REMOVE LINE12 REMOVE LINE13 LINE 7 ABCDH LINE 8 EFGAG LINE 9 HCJKD I have several such files. I...
I have a file **(FILE1)** with some repeated sections as below (example):
LINE 1 ABCD
LINE 2 EFGA
LINE 3 HCJK
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
LINE 4 ABCDH
LINE 5 EFGAG
LINE 6 HCJKD
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
LINE 7 ABCDH
LINE 8 EFGAG
LINE 9 HCJKD
I have several such files. In a pattern file (**PATTERN**) I have these removable lines stored.
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
I want to write a sed, awk (bash code) or a Perl code to remove all the sections of of **FILE** that match the content of the file **PATTERN**. Another requirement is to remove all but leave the first occurrence only.
Pratap
(47 rep)
Aug 6, 2025, 06:03 AM
• Last activity: Aug 6, 2025, 03:31 PM
2
votes
3
answers
7476
views
Parse value from different JSON strings (No jq)
I'm using a bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the `Content` or from any other variable. Such as, Lookup `Content` and be able to print `Value1`. Lookup `DeviceType` a...
I'm using a bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the
Content
or from any other variable. Such as,
Lookup Content
and be able to print Value1
.
Lookup DeviceType
and be able to print Value4
.
Sample Input:
{"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}
I tried the combination of sed
and awk
:
sed 's/["]/ /g' | awk '{print $4}'
... but only if the position of Content
remains the same in the output. Otherwise, in the different JSON output, the positioning of Content
changes that puts the value out of scope thus,
awk '{print $4}'
picks up the wrong value.
Riz
(59 rep)
Nov 8, 2019, 03:46 PM
• Last activity: Aug 3, 2025, 12:04 AM
101
votes
13
answers
188281
views
How to print all lines after a match up to the end of the file?
Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in `other file` ( like `dog 123 4335` from file2). I match the pattern of the line is `dog 123 4335` and after printing all lines without match line my output is: cat 13123 23424 deer 2131...
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in
other file
( like dog 123 4335
from file2).
I match the pattern of the line is dog 123 4335
and after printing
all lines without match line my output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
If only use without address of line only use the pattern, for example 1s
how to match and print the lines?
loganaayahee
(1209 rep)
Nov 23, 2012, 07:17 AM
• Last activity: Aug 2, 2025, 07:26 AM
214
votes
7
answers
755303
views
Return only the portion of a line after a matching pattern
So pulling open a file with `cat` and then using `grep` to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a pattern, but only to return the portion of the line after the match. The portion before and after...
So pulling open a file with
cat
and then using grep
to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a pattern, but only to return the portion of the line after the match. The portion before and after the match will consistently vary. I have played with using sed
or awk
, but have not been able to figure out how to filter the line to either delete the part before the match, or just return the part after the match, either will work.
This is an example of a line that I need to filter:
2011-11-07T05:37:43-08:00 isi-udb5-ash4-1(id1) /boot/kernel.amd64/kernel: [gmp_info.c:1758](pid 40370="kt: gmp-drive-updat")(tid=100872) new group: : { 1:0-25,27-34,37-38, 2:0-33,35-36, 3:0-35, 4:0-9,11-14,16-32,34-38, 5:0-35, 6:0-15,17-36, 7:0-16,18-36, 8:0-14,16-32,34-36, 9:0-10,12-36, 10-11:0-35, 12:0-5,7-30,32-35, 13-19:0-35, 20:0,2-35, down: 8:15, soft_failed: 1:27, 8:15, stalled: 12:6,31, 20:1 }
The portion I need is everything after "stalled".
The background behind this is that I can find out how often something stalls:
cat messages | grep stalled | wc -l
What I need to do is find out how many times a certain node has stalled (indicated by the portion before each colon after "stalled". If I just grep for that (ie 20:) it may return lines that have soft fails, but no stalls, which doesn't help me. I need to filter only the stalled portion so I can then grep for a specific node out of those that have stalled.
For all intents and purposes, this is a freebsd system with standard GNU core utils, but I cannot install anything extra to assist.
MaQleod
(2734 rep)
Nov 7, 2011, 11:18 PM
• Last activity: Aug 1, 2025, 11:13 AM
0
votes
3
answers
1964
views
sed? - Insert line after a string with special characters to Neutron service
I am attempting to write a bash script that will insert a string after matching on a string in /usr/lib/systemd/system/neutron-server.service I have been able to do this on other files easily as I was just insert variables into neccessary config files, but this one seems to be giving me trouble. I b...
I am attempting to write a bash script that will insert a string after matching on a string in
/usr/lib/systemd/system/neutron-server.service
I have been able to do this on other files easily as I was just insert variables into neccessary config files, but this one seems to be giving me trouble.
I believe the error is that sed is not ignoring the special characters. In my attempt I have tried using sed of single quotes and double quotes (which I understand are for variables, but thought it might change something. Is there a better way of going about this or some special sed flags or syntax I am missing?
sed ‘/--config-file /etc/neutron/plugin.ini/a\--config-file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini‘ /usr/lib/systemd/system/neutron-server
TL;DR - Insert
--config-file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini
After
--config-file /etc/neutron/plugin.ini
Orginial File
[Unit]
Description=OpenStack Neutron Server
After=syslog.target network.target
[Service]
Type=notify
User=neutron
ExecStart=/usr/bin/neutron-server --config-file /usr/share/neutron/neutron-
dist.conf --config-dir /usr/share/neutron/server --config-file
/etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini --config-dir
/etc/neutron/conf.d/common --config-dir /etc/neutron/conf.d/neutron-server -
-log-file /var/log/neutron/server.log
PrivateTmp=true
NotifyAccess=all
KillMode=process
TimeoutStartSec="infinity"
[Install]
WantedBy=multi-user.target
File after desired change command.
[Unit]
Description=OpenStack Neutron Server
After=syslog.target network.target
[Service]
Type=notify
User=neutron
ExecStart=/usr/bin/neutron-server --config-file /usr/share/neutron/neutron-
dist.conf --config-dir /usr/share/neutron/server --config-file
/etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini --config-
file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini --config-dir
/etc/neutron/conf.d/common --config-dir /etc/neutron/conf.d/neutron-server -
-log-file /var/log/neutron/server.log
PrivateTmp=true
NotifyAccess=all
KillMode=process
TimeoutStartSec="infinity"
[Install]
WantedBy=multi-user.target
fly2809
(1 rep)
Jun 13, 2017, 08:48 PM
• Last activity: Jul 22, 2025, 09:04 AM
-1
votes
3
answers
62
views
How to remove markdown links from headers with sed?
I'm trying to use sed to remove links like and leave just the title: ```markdown ## [Some title](#some-title) ``` This is my command: ```bash sed 's/^\(\#*\) *\[\([^\]]*\)\].*/\1 \2/' ``` I expect to have just the text without the link: ```markdown ## Some title ``` But it doesn't work. What I do wr...
I'm trying to use sed to remove links like and leave just the title:
## [Some title](#some-title)
This is my command:
sed 's/^\(\#*\) *\[\([^\]]*\)\].*/\1 \2/'
I expect to have just the text without the link:
## Some title
But it doesn't work. What I do wrong?
I'm using Linux with GNU sed.
jcubic
(10310 rep)
Jul 17, 2025, 11:35 PM
• Last activity: Jul 19, 2025, 06:22 AM
1
votes
6
answers
602
views
Change a double quote into a single quote with gnu sed when the quotes come before and after a parenthesis?
I'm using Debian 12 bookworm, gnu sed. I have this ```lang-none "Hello1" "('41-Z', 5001, 'A6')" "'Hello2'" "('42-Z', 5002, 'A7')" 'Hello3' "('43-Z', 5003, 'A8')" ``` I want to modify only the lines with parenthesis entries, to appear like this: ```lang-none "Hello1" '('41-Z', 5001, 'A6')' "'Hello2'"...
I'm using Debian 12 bookworm, gnu sed. I have this
-none
"Hello1"
"('41-Z', 5001, 'A6')"
"'Hello2'"
"('42-Z', 5002, 'A7')"
'Hello3'
"('43-Z', 5003, 'A8')"
I want to modify only the lines with parenthesis entries, to appear like this:
-none
"Hello1"
'('41-Z', 5001, 'A6')'
"'Hello2'"
'('42-Z', 5002, 'A7')'
'Hello3'
'('43-Z', 5003, 'A8')'
How can I used sed to trade out the double quotes for single quotes of the lines in this example that have parenthesis, without interfering with the quotes of the other lines? This is a large text file that has hundreds, maybe thousands of these entries.
I prefer sed, but I also tried looking for answers that use tr (truncate). Maybe someone has an awk answer.
shrykullgod
(39 rep)
Jul 13, 2025, 11:18 PM
• Last activity: Jul 16, 2025, 10:07 AM
1
votes
2
answers
93
views
How to extract a sub-heading as string which is above a search for word
I'm new to Bash and I've been self-taught. I think I'm learning well, but I do have staggering gaps in my base knowledge. So sorry if this is woefully simple bbuuuttt... Essentially, I need to sift through a large amount of data and pull out specific phrases. I've been making slow and steady progres...
I'm new to Bash and I've been self-taught. I think I'm learning well, but I do have staggering gaps in my base knowledge. So sorry if this is woefully simple bbuuuttt...
Essentially, I need to sift through a large amount of data and pull out specific phrases. I've been making slow and steady progress, but I'm now stuck on getting a heading for a line of data.
Here's what the file looks like:
A lot (AND I MEAN A LOT) of data above
STATE 1:
133a -> 135a : 0.010884 (c= -0.10432445)
134a -> 135a : 0.933650 (c= -0.96625573)
STATE 2:
129a -> 135a : 0.016601 (c= -0.12884659)
130a -> 135a : 0.896059 (c= -0.94660402)
130a -> 136a : 0.011423 (c= 0.10687638)
130a -> 137a : 0.023884 (c= -0.15454429)
130a -> 138a : 0.020361 (c= -0.14269354)
STATE 3:
133a -> 135a : 0.899436 (c= -0.94838591)
134a -> 136a : 0.012334 (c= -0.11106052)
STATE 4:
129a -> 135a : 0.688049 (c= -0.82948703)
129a -> 136a : 0.212819 (c= -0.46132295)
129a -> 137a : 0.036987 (c= 0.19231930)
130a -> 135a : 0.011990 (c= 0.10949722)
134a -> 135a : 0.922010 (c= -0.98192034)
There are many more states (up to 30) of varying length below, which may also include what I'm looking for.
And then more data below that
I have got the numbers I am looking for saved in variables. (134 and 135 for this example) And I can use :
"${a}a -> ${b}a" File.Name;
to show me the lines that have 134 -> 135 on, but I need the STATE that they are in.
I've tried using grep to look above the found lines to the nearest line with STATE in, but I couldn't figure out how to set the length of -B as a condition rather than a number (don't know if it can be done). I have also tried with awk and sed to find the line with STATE and look below to see if 134 -> 135 is benethe it before the next STATE, but I couldn't find a way to stop it and not print at the next STATE instead of just continuing until it found the next 134 -> 135. The Ideal output (for the above example) would be:
STATE 1
STATE 4
but
STATE 1:
133a -> 135a : 0.010884 (c= -0.10432445)
134a -> 135a : 0.933650 (c= -0.96625573)
STATE 4:
129a -> 135a : 0.688049 (c= -0.82948703)
129a -> 136a : 0.212819 (c= -0.46132295)
129a -> 137a : 0.036987 (c= 0.19231930)
130a -> 135a : 0.011990 (c= 0.10949722)
134a -> 135a : 0.922010 (c= -0.98192034)
is also absolutely fine. I just need it to spit out the correct STATES and no others. It doesn't really matter what other data comes with it.
Also, this is going to be applied to about 40 other files with similar layouts, so I need it not to be specific to this one (aka not grep STATE 1 and grep state 4)
I'm hoping someone can help me or tell me if this is impossible to do.
TC575
(13 rep)
Jul 11, 2025, 08:37 PM
• Last activity: Jul 16, 2025, 09:53 AM
2
votes
5
answers
373
views
How to replace apache directory config with sed?
I'm trying to replace a Directory option from apache configuration with `sed` replace command, but I cannot understand why it doesn't work; it changes nothing. What I'm missing? This is the config example: # Sets the default security model of the Apache2 HTTPD server. It does # not allow access to t...
I'm trying to replace a Directory option from apache configuration with
sed
replace command, but I cannot understand why it doesn't work; it changes nothing. What I'm missing?
This is the config example:
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
Options FollowSymLinks
AllowOverride None
Require all denied
AllowOverride None
Require all granted
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
And this is my sed command, which unfortunately makes no changes:
sed -E 's/()/\1All\2/' /etc/apache2/apache2.conf
(-i option is missing for testing purposes)
My goal is to set AllowOverride All
only to `` sentence.
Tobia
(131 rep)
May 29, 2025, 07:47 AM
• Last activity: Jul 15, 2025, 08:35 AM
6
votes
5
answers
1500
views
Summing rows in a new column using sed, awk and perl?
I have a file that contain numbers something like: 1 11 323 2 13 3 3 44 4 4 66 23 5 70 23 6 34 23 7 24 22 8 27 5 How can I sum the rows and output the results in a column, so the results are as follows: 1 11 323 335 2 13 3 18 3 44 4 51 4 66 23 93 5 70 23 98 6 34 23 63 7 24 22 53 8 27 5 40 I would li...
I have a file that contain numbers something like:
1 11 323
2 13 3
3 44 4
4 66 23
5 70 23
6 34 23
7 24 22
8 27 5
How can I sum the rows and output the results in a column, so the results are as follows:
1 11 323 335
2 13 3 18
3 44 4 51
4 66 23 93
5 70 23 98
6 34 23 63
7 24 22 53
8 27 5 40
I would like to see solutions in sed, awk, and perl
user311543
Sep 19, 2018, 03:07 PM
• Last activity: Jul 14, 2025, 02:54 AM
-1
votes
2
answers
135
views
Fixing sed expression
I'm using "hosts" files from different sources and to unblock some sites i use the following SED code: sed -i '/0.0.0.0 internet.com/s/^/#/g' /etc/hosts Result: It puts # in front of 0.0.0.0 internet.com but also 0.0.0.0 internet.com.site I need to fix it.
I'm using "hosts" files from different sources and to unblock some sites i use the following SED code:
sed -i '/0.0.0.0 internet.com/s/^/#/g' /etc/hosts
Result:
It puts # in front of
0.0.0.0 internet.com
but also
0.0.0.0 internet.com.site
I need to fix it.
superbyte
(45 rep)
Jun 2, 2019, 07:39 PM
• Last activity: Jul 13, 2025, 07:04 PM
0
votes
5
answers
2465
views
Shell Script: Want to delete two consecutive lines matching pattern from specific line
I want to delete specific two consecutive lines matching patterns from specific line from a file. For e.g. file contents are like below. a b c Name: 123 xyz Name: 456 abc I want to find the lines starting from line 4, matching 1st line pattern starting with `Name: ` and matching 2nd line pattern sta...
I want to delete specific two consecutive lines matching patterns from specific line from a file.
For e.g. file contents are like below.
a
b
c
Name: 123
xyz
Name: 456
abc
I want to find the lines starting from line 4, matching 1st line pattern starting with
Name:
and matching 2nd line pattern starting with whitespace and delete the two consecutive lines.
Any efficient way to do this in shell using sed
or something else?
To be a bit more clear, I want to remove signing/checksum information from the MANIFEST.MF
.
Sample MANIFEST.MF
like below. From the below manifest file, I want to remove the entry Name:
where Name:
entry can be in one line or 2(or more) lines.
Initially my solution was like find the first Name:
entry followed by SHA-256-Digest:
entry and delete to the end of the file. Unfortunately this solution has a problem of removing one needed entry in the middle. For e.g. NetBeans-Simply-Convertible:
is also being removed.
So, now I want to remove Name:
entry if available in 1 line or entry spanned across 2 or more lines. But I should not lose entries like NetBeans-Simply-Convertible:
while removing Name:
entries.
Already I am removing SHA-256-Digest:
entries with the below command on file with sed -i "/^\SHA-256-Digest: /d" $manifest_file
--------------------------------
Manifest-Version: 1.0
Version-Info: ....
Name: com/abc/xyz/pqr/client/relationship/message/notifier/Relati
onshipUpdateNotifierFactory.class
SHA-256-Digest: cSSyk6Y2L2F9N6FPtswUkxjF2kelMkGe4bFprcQ+3uY=
Name: com/abc/xyz/pqr/client/relationship/ui/BaseRelationshipView
$5.class
SHA-256-Digest: w9HgRjDuP024U4CyxeKPYFe6rzuzxZF3b+9LVG36XP8=
Name: com/abc/xyz/pqr/client/impl/MofRelationshipAgentImpl.class
SHA-256-Digest: GwIBIU+UdPtjyRhayAVM90Eo+SwCT/kP65dI59adEnM=
Name: com/abc/xyz/pqr/client/settings/ConvertibleProperties.class
NetBeans-Simply-Convertible: {com/abc/xyz/pqr/client/settings}Con
vertibleProperties
SHA-256-Digest: 5FszAtfpPXcLx/6FBWbfeg6E4fwFMRozV+Q+3rReATc= ...
Expected Output:
Manifest-Version: 1.0
Version-Info: ....
NetBeans-Simply-Convertible: {com/abc/xyz/pqr/client/settings}Con
vertibleProperties
...
onlysrinivas
(1 rep)
Apr 15, 2017, 06:15 AM
• Last activity: Jul 11, 2025, 09:39 AM
0
votes
5
answers
569
views
How can I `cat` files with a number of fixed lines before/between/after?
I am looking for a bash one-liner which can `cat` a number of files with a number of fixed lines. file1.txt: file1 line 1 file1 line 2 file2.txt file2 line 1 file2 line 2 Then I am looking for something like cat-with-strings foo file1.txt bar file2.txt baz producing output foo file1 line 1 file1 lin...
I am looking for a bash one-liner which can
cat
a number of files with a number of fixed lines.
file1.txt:
file1 line 1
file1 line 2
file2.txt
file2 line 1
file2 line 2
Then I am looking for something like
cat-with-strings foo file1.txt bar file2.txt baz
producing output
foo
file1 line 1
file1 line 2
bar
file2 line 1
file2 line 2
baz
How can I do this in a single line of bash, using standard linux tools (sed, awk, cat, etc) and *without* creating any files to hold foo
, bar
, or baz
?
spraff
(951 rep)
Jan 25, 2021, 07:00 PM
• Last activity: Jul 7, 2025, 08:30 AM
39
votes
4
answers
88652
views
Replace text quickly in very large file
I have 25GB text file that needs a string replaced on only a few lines. I can use `sed` successfully but it takes a really long time to run. sed -i 's|old text|new text|g' gigantic_file.sql Is there a quicker way to do this?
I have 25GB text file that needs a string replaced on only a few lines. I can use
sed
successfully but it takes a really long time to run.
sed -i 's|old text|new text|g' gigantic_file.sql
Is there a quicker way to do this?
eisaacson
(491 rep)
Jan 14, 2016, 07:14 PM
• Last activity: Jul 4, 2025, 08:19 AM
158
votes
20
answers
171738
views
Decoding URL encoding (percent encoding)
I want to decode URL encoding, is there any built-in tool for doing this or could anyone provide me with a `sed` code that will do this? I did search a bit through [unix.stackexchange.com][1] and on the internet but I couldn't find any command line tool for decoding url encoding. What I want to do i...
I want to decode URL encoding, is there any built-in tool for doing this or could anyone provide me with a
sed
code that will do this?
I did search a bit through unix.stackexchange.com and on the internet but I couldn't find any command line tool for decoding url encoding.
What I want to do is simply in place edit a txt
file so that:
- %21
becomes !
- %23
becomes #
- %24
becomes $
- %26
becomes &
- %27
becomes '
- %28
becomes (
- %29
becomes )
And so on.
DisplayName
(12016 rep)
Oct 4, 2014, 01:13 PM
• Last activity: Jul 3, 2025, 05:20 PM
3
votes
2
answers
400
views
Get Length of Function in Shell
**UPDATE:** **Some Background:** `zsh` returns the line number inside a function when `$LINENO` is called inside a function. I need a way to get the line number in the file and to differentiate when `zsh` is giving me a file line number vs. a function line number. I couldn't find a `zsh` environment...
**UPDATE:**
**Some Background:**
zsh
returns the line number inside a function when $LINENO
is called inside a function. I need a way to get the line number in the file and to differentiate when zsh
is giving me a file line number vs. a function line number.
I couldn't find a zsh
environment variable to change this behavior to match other Bourne shells (e.g. bash
always gives the file line number), so I was trying to see if I could create a function with logic that could always output the file line number regardless of context. This is why I was trying to determine the length of the function.
If anyone knows of a good way to get the file line number with $LINENO
in zsh
in all contexts, I'd appreciate it!
---
**QUESTION:**
I've searched this and this , but can't seem to find an answer. Is there a portable way to write the number of lines a function definition has? *(Please see "Some Background" above.)*
My initial thought was to capture the function contents and pipe it to wc -l
.
Consider the following test file:
**Test File:**
sh
#! /bin/sh
#
# test_file.sh
func1() { echo 'A one-liner'; } # With a nasty comment at the end
func2 (){
echo "A sneaky } included"
# Or an actual code block
{
echo 'hi'
echo 'there'
}
}
func3() { echo "I'm on a line."; }; echo 'And so am I'
func4(){ echo "But I'm a \"stand-alone\" one-liner."; }
func5() {
echo "I'm a nice function."
echo "And you can too!"
}
echo "Can we do this?"
My initial attempt was to match corresponding pairs of {}'s with sed:
**Solution Attempt:**
sh
#! /bin/sh
#
# function_length
#
# $1: string: absolute path to file
# $2: string: name of function (without ()'s)
fp=$(realpath "$1")
func_name="$2"
func_contents=$(cat "${fp}" |
sed -E -n '
/'"${func_name}"' ?[(][)]/{
:top
/[}]/!{
H
d
}
/[}]/{
x
s/[{]//
t next
G
b end
}
:next
x
b top
:end
p
q
}')
echo "${func_contents}"
echo
func_len=$(echo "${func_contents}" | wc -l)
echo "Function Length: ${func_len}"
However, running this in zsh gives
sh
$ ./function_length ./test_file.sh func1
func1() { echo 'A one-liner'; } # With a nasty comment at the end
Function Length: 2
$ ./function_length ./test_file.sh func2
Function Length: 1
$ ./function_length ./test_file.sh func3
func3() { echo "I'm on a line."; }; echo 'And so am I'
Function Length: 2
$ ./function_length ./test_file.sh func4
func4(){ echo "But I'm a \"stand-alone\" one-liner."; }
Function Length: 2
$ ./function_length ./test_file.sh func5
Function Length: 1
Does anyone know of a solution? Thank you!
adam.hendry
(243 rep)
Jun 13, 2021, 09:13 PM
• Last activity: Jun 28, 2025, 01:30 PM
65
votes
1
answers
148079
views
find & sed (search and replace)
I'm using the following command on my mac: $find . -name “*.java” -exec sed -i ’s/foo/bar/g’ {} \; and it seems to have no effect. I have two files in the directory that end in .java, which both have the foo text in them. Am I missing something? **EDIT : Results from request of comments** [aafghani-...
I'm using the following command on my mac:
$find . -name “*.java” -exec sed -i ’s/foo/bar/g’ {} \;
and it seems to have no effect.
I have two files in the directory that end in .java, which both have the foo text in them. Am I missing something?
**EDIT :
Results from request of comments**
[aafghani-03:~/test amirafghani]$ find . -name "*.java" -exec sed -i 's/foo/bar/g' {} \;
sed: 1: "./bar.java": invalid command code .
sed: 1: "./foo.java": invalid command code .
Amir Afghani
(7373 rep)
Apr 18, 2012, 06:59 PM
• Last activity: Jun 28, 2025, 10:35 AM
6
votes
5
answers
2245
views
How to make grep for a regex that appear multiple times in a line
I want to grep a regex. The pattern I am searching for may appear multiple times in a line. If the pattern appeared many times, I want to separate each occurrence by a comma and print **the match only** not the full line in a new file. If it did not appear in a line I want to print **n.a.** Example....
I want to grep a regex. The pattern I am searching for may appear multiple times in a line. If the pattern appeared many times, I want to separate each occurrence by a comma and print **the match only** not the full line in a new file. If it did not appear in a line I want to print **n.a.**
Example. I want to use this regex to find numbers in the pattern: [12.123.1.3].
grep -oh "\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]" 'filename'
input file (input.txt)
blabla [11.335.2.33] xyuoeretrete [43.22.11.88] jfdfjkfbs [55.66.77.88]
blabla [66.223.44.33]
foo bar
blabla [1.2.33.3] xyuoeretrete bla[1.32.2.4]
intended result in a new file (output.csv):
11.335.2.33,43.22.11.88,55.66.77.88
66.223.44.33
n.a.
1.2.33.3,1.32.2.4
**Note: I use Ubuntu**
randomname
(161 rep)
Jun 24, 2022, 09:08 AM
• Last activity: Jun 24, 2025, 09:28 AM
4
votes
5
answers
5991
views
Print only unique lines from file not the duplicates
I have a sorted words list, line by line file like so: apple apple grapes lime orange orange pear pear peach strawberry strawberry I only want to print out unique lines and drop duplicates: grapes peach lime How can I do this with `sed`, `awk` or `perl`?
I have a sorted words list, line by line file like so:
apple
apple
grapes
lime
orange
orange
pear
pear
peach
strawberry
strawberry
I only want to print out unique lines and drop duplicates:
grapes
peach
lime
How can I do this with
sed
, awk
or perl
?
user155704
Feb 9, 2016, 02:36 PM
• Last activity: Jun 16, 2025, 01:34 AM
1
votes
3
answers
326
views
Remove lines matching pattern, plus any lines following it matching a different pattern
Let me preface I am not sure if this question has been asked before, I have been Googling for answers but came up short. I want to use standard Linux/Unix commands (running this on FreeBSD) to exclude lines from a log file that match a pattern. The log file also includes "last message repeated x tim...
Let me preface I am not sure if this question has been asked before, I have been Googling for answers but came up short.
I want to use standard Linux/Unix commands (running this on FreeBSD) to exclude lines from a log file that match a pattern. The log file also includes "last message repeated x times" to condense log entries.
As an example, I want to take this:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:08 useless information #1
May 27 2023 11:08 last message repeated 5 times
May 27 2023 11:09 last message repeated 8 times
May 27 2023 11:09 relevant information #3
May 27 2023 11:09 useless information #2
May 27 2023 11:10 useless information #3
May 27 2023 11:10 last message repeated 6 times
And get this output:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:09 relevant information #3
I've gotten as far as using sed commands to do this, but I don't know enough about how it works to figure it out. I am especially lost when it comes to the log lines that have multiple "last message repeated" following it. Here's what I'm working with currently:
sed '/useless information/{d;N;/last message repeated/d;}' ./logfile.txt
The above first deletes matching lines containing "useless information", then adds the next line to the namespace with N
, and then is supposed to delete the resulting line if it contains "last message repeated". But it is only deleting the lines with "useless information".
ekrekeler
(13 rep)
May 27, 2023, 04:49 PM
• Last activity: Jun 15, 2025, 04:20 AM
Showing page 1 of 20 total questions