LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Know the current filename sed is processing and to inform it to shell (https://www.linuxquestions.org/questions/linux-general-1/know-the-current-filename-sed-is-processing-and-to-inform-it-to-shell-4175679398/)

BudiKusasi 07-26-2020 05:09 AM

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.

Turbocapitalist 07-26-2020 05:33 AM

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.

shruggy 07-26-2020 05:38 AM

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

Turbocapitalist 07-26-2020 05:42 AM

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.

BudiKusasi 07-26-2020 05:45 AM

SOLVED by @shruggy

shruggy 07-26-2020 05:51 AM

@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

Turbocapitalist 07-26-2020 06:03 AM

Quote:

Originally Posted by shruggy (Post 6149414)
@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"


shruggy 07-26-2020 06:10 AM

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.

Turbocapitalist 07-26-2020 06:19 AM

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.

shruggy 07-26-2020 06:31 AM

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.

MadeInGermany 07-27-2020 07:24 AM

Something else:
Code:

echo *.txt |xargs sed ...
is (too) complicated for
Code:

sed ... *.txt


All times are GMT -5. The time now is 05:18 PM.