LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-26-2020, 05:09 AM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Know the current filename sed is processing and to inform it to shell


How to know the current filename sed is processing and to inform it to shell environment variable

Find in all .txt files line with NA end if any.
If any then remove the line and quit with exit 0 (shell true), else exit any error code

Code:
echo *.txt |xargs sed -Ei '/NA$/{d; q0}; q1 ' .....
but how to get the file name if the condition is true to be echoing it (elipsis means the required answer on continuation) and won't echo it if it's false ?

Thanks.

Last edited by BudiKusasi; 07-26-2020 at 05:10 AM.
 
Old 07-26-2020, 05:33 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The echo is passing the literal string '*.txt' to xargs and it is sed itself which ends up globbing. So a very different approach is needed, if the above illustrated your model correctly. Also, the sed script you have truncates the files instead of just removing the first occurence of a line with the pattern.

Code:
for f in *.txt;
do;
        sed -i.orig -E -n '/NA$/{s/.*/foo/;h;d};p;${g;/foo/{q0};q1}' $f;
        echo $?;
done;
The -i option should usually be given a string to append to the file name so that the original files are kept. That way there is a backup copy in the event of a mistake. However there are other ways to backup the files, such as tar, too.
 
Old 07-26-2020, 05:38 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
From the sed manual:
Quote:
F
Print out the file name of the current input file (with a trailing newline).
Code:
sed -Ei '/NA$/{F;s/.*/FOUND/;h;d;b};${x;s/^FOUND$//;x;t;q1}' *.txt

Last edited by shruggy; 07-26-2020 at 06:51 AM.
 
1 members found this post helpful.
Old 07-26-2020, 05:42 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The F function looks like the right choice, when it exists. However which version of Sed is it in? It's not in GNU sed 4.7, for example,

Code:
$ sed --version | sed '1q'
sed (GNU sed) 4.7
nor in OpenBSD's sed.
 
Old 07-26-2020, 05:45 AM   #5
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Original Poster
Rep: Reputation: 15
SOLVED by @shruggy

Last edited by BudiKusasi; 07-26-2020 at 05:46 AM.
 
Old 07-26-2020, 05:51 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
@Turbocapitalist. The code for F was committed to GNU sed on 2009-11-15:
Quote:
* Noteworthy changes in release 4.2.2 (2012-12-22) [stable]
[...]
* New command `F' to print current input file name

Last edited by shruggy; 07-26-2020 at 05:53 AM.
 
1 members found this post helpful.
Old 07-26-2020, 06:03 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by shruggy View Post
@Turbocapitalist. The code for F was committed to GNU sed on 2009-11-15:
Oh. On at least some systems, it is missing from the manual page. :/

Code:
$ dpkg -S /bin/sed
sed: /bin/sed

$ apt-cache policy sed | sed '4,$d'
sed:
  Installed: 4.7-1
  Candidate: 4.7-1

$ grep -i pretty /etc/os-release 
PRETTY_NAME="Ubuntu 20.04.1 LTS"
 
Old 07-26-2020, 06:10 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Well, manual pages for GNU tools are not authoritative. (Sometimes, they're even not from the upstream, but created by Debian maintainers.) You should look it up in the info manual.
 
Old 07-26-2020, 06:19 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,330
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Subjectively speaking, I've never found anything of use in the info "manuals", most of which are placeholders, and the interface for them is innavigable. Nothing good to say about them except that they seem to have died on the vine and are going away.

I'm disappointed that the manual pages are so neglected. They should be the penultimate authority, second only to the source code.
 
Old 07-26-2020, 06:31 AM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I wish it were as you described. Unfortunately, citing the GNU Coding Standards:
Quote:
6.9 Man Pages

In the GNU project, man pages are secondary. It is not necessary or expected for every GNU program to have a man page, but some of them do. It’s your choice whether to include a man page in your program.

When you make this decision, consider that supporting a man page requires continual effort each time the program is changed. The time you spend on the man page is time taken away from more useful work.
This obviously clashes with the following Debian Policy requirement:
Quote:
Each program, utility, and function should have an associated manual page included in the same package. It is suggested that all configuration files also have a manual page included as well. Manual pages for protocols and other auxiliary things are optional.

If no manual page is available, this is considered as a bug and should be reported to the Debian Bug Tracking System (the maintainer of the package is allowed to write this bug report themselves, if they so desire). Do not close the bug report until a proper man page is available.

You may forward a complaint about a missing man page to the upstream authors, and mark the bug as forwarded in the Debian bug tracking system. Even though the GNU Project do not in general consider the lack of a man page to be a bug, we do; if they tell you that they don’t consider it a bug you should leave the bug in our bug tracking system open anyway.

Last edited by shruggy; 07-26-2020 at 06:34 AM.
 
1 members found this post helpful.
Old 07-27-2020, 07:24 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Something else:
Code:
echo *.txt |xargs sed ...
is (too) complicated for
Code:
sed ... *.txt
 
  


Reply

Tags
sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to know what are the snmp traps/alerts a SNMP device can send/inform ? marozsas Linux - Networking 5 11-06-2019 11:48 AM
LXer: BBC Micro retrospective and the Raspberry Pi – educate, inform and entertain LXer Syndicated Linux News 0 03-04-2014 02:11 AM
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM
BASH script inform user and kill process mounters Programming 3 02-11-2002 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration