Tag Info

New answers tagged

2

With the zsh shell, you can affect the order of globs with glob qualifiers. grep 'exit 0' a*(.Om) Om is to reverse o⃞rder by m⃞odification time. I also added .⃞ to select only regular files (not directories or pipes or devices or symlinks...).


-2

Thanks Omni. Even the following way, I am able to achieve this. Of course urs is the simple one. thanks a lot grep -H "exit 0" /opt/ctmagent/ctm/sysout_archives/141027/d_dms_btch_alrt_ifm_scp_ksh* | awk -F":" '{system("ls -latr "$1"")}'


1

If none of the file names contain space, tab, newline, ?, *, [ characters or are called -, this should do it: grep "exit 0" -- $(ls -tr a*) $() is called command substitution. Your shell will first run ls -tr, then use the output as the list of files for your grep command.


0

There are several problems, including: Your code as posted doesn't hang You use the -l flag to ls, which means you'll be looking for files named -rw-r--r-- and such You should avoid parsing the output of ls at all And there's no need to test the output of grep, you can just use the exit status. From man grep: EXIT STATUS The exit status is 0 if ...


2

You could simplify this significantly (and avoid the nasty problems inherent in parsing ls) by doing something like: file_nm=$(find . -maxdepth 1 -name "*abc*"); [ -e "$file_nm" ] && grep -q "search_txt" "$file_nm" && echo "string found" || echo "string not found" However, like your original example, that will fail if you have ...


0

Try: grep "search_txt" /dev/null "$file_nm" Feeding /dev/null to grep will make it work even if $file_nm is not existed, because grep always has at least one file - which is /dev/null - to work on.


0

You should test if $file_nm is empty before running the grep if [ -n "$file_nn" ]; will return True if $file_nm is NOT empty.


1

You could use sed like this: sed 'h;s/,[^|]*//g;x /,/{s/|[^,|]*,*/|-/g;H };x;s/-\([^|]\)/\1/g;P;D' It wound up being relatively simple after all. Applying that little script to your data gets: key1|0|11881|0|0|0|0|11769|0|0|0 key2|2027|345|0|1|0|2040|364|0|1|0 key2|-|712|-|-|-|-|729|-|-|- key3|0|670944|0|0|0|0|495554|0|0|0 ...


1

I would not use awk for this, because I think it needs more flexible data structures. I would use perl: perl -MList::Util=max -F'\|' -lane ' $key = shift @F; @data = map {[split /,/]} @F; do { @row = map {shift(@$_) // "-"} @data; print join("|", $key, @row); $max = max map {scalar @$_} @data; } while ($max > 0); ' ...


1

You were close, just unnecessarily complicated this task. Try awk -f script.awk main.txt index.txt (notice reverse order of files) with the following script: #!/bin/awk BEGIN { FS = "|" } ( NR == FNR ) { lookup[toupper($1)] = $0 } ( NR > FNR ) { key = toupper($1) n=split(lookup[key], replacements, "|") replacements[$2+1]=$3 for ...


3

There is a way in bash 4.3+, which probably comes from ksh: echo_idx_array () # array index { local -n array=$1 # add nameref attribute local idx=$2 echo "${array[idx]}" } $ names=(one two three four) $ echo_idx_array names 2 three $ days=([monday]=eggs [tuesday]=bread [sunday]=jam) # associative array $ echo_idx_array days sunday jam ...


10

A few pieces of documentation will help to explain this. From the POSIX standards document for the shell: The following variables shall affect the execution of the shell: PS1: Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. ... ...


4

The \ character escapes the following (special) character. In this case, it escapes the $, which we usually use to dereference a variable. When the shell evaluates a variable assignment, it first expands the right-hand-side of the expression. Without the \ before $PWD, the shell expands $PWD and assigns the result to PS1. However, with the \, the shell ...


2

From the documentation: Functions defined by the function name syntax and called by name execute in the same process as the caller and share all files and present working directory with the caller. Traps caught by the caller are reset to their default action inside the function. Whereas Functions defined with the name() ...


2

You need to either replace the | with && or let the script output the data to stdout and use |. When using &&, the mail command would only be run when the script exits with 0. When using the pipe character, your script needs to send the data to stdout so the pipe can pass it on to mail. In what you wrote, the script writes data to the file ...


3

slm's answer here hasn't taken into account that you asked about the Korn shell, not about the Bourne Again shell. The (93) Korn shell has no built-in expr command, so when using expr in Korn shell scripts you are using an external expr command. This is not a problem per se. After all, it's how one did things with the Bourne shell, which also had no expr ...


3

You can use the built in arithmetic support in the shell that is used by a double parenthesis preceded by a dollar sign: $(( (first + second + third) / 3)) This is more efficient than expr since it does not require a seperate process (and hence fork, exec, pipe management and return).


2

Your question originally did not have the Korn Shell tag so I've since added it. If you were using Bash the following would apply. The command expr can perform a variety of mathematical calculations. Simply make use of it to perform the averaging as well. $ AVG=$(expr $SUM / 3) Also you'll typically want to make use of this form of sub commands, ...



Top 50 recent answers are included