Tag Info

New answers tagged

0

what about awk 'FNR==1' A_*.csv > Header.csv where FNR is File Number Record default action is to print the while line I dropped -F, as you don't care about individual field (If you have other thing to do, however, you can add it back)


0

Something like this will work: for file in `ls A_*.csv` do awk -F, 'NR==1 {print $0}' $file >> Header.csv done This is basic shell scripting looping. You can find these tutorials online, if you search for them.


1

If you want to avoid re-inventing the wheel, you could use the mv command's built in ability to do automatic numbered backups; if your shell supports the case conversion natively that could be as simple as for f in *; do mv --backup=numbered -- "$f" "${f,,}"; done The default backup number format is .~1~, for example given SOME FILE sOmE fIlE some file ...


3

As one who has worked in a multitude of ?nix environments, I have had to write in a wide variety of shells. Believe it or not, across platforms, the shells are not the same. So if you maintain your personal library in multiple shells (when necessary) it is very helpful to use extensions to ID the shells. That way when you move to another platform and the ...


0

A dirty solution would look like: boolRememberNext=0 rememberString="" for arg in $*; do if [ ${boolRememberNext} -eq 1 ]; then rememberString="${rememberString} ${arg}" fi if [ "${arg}" = "-name" ]; then boolRememberNext=1 else boolRememberNext=0 fi done echo ${rememberString} | while read filemask; do ...


1

Bash makes it relatively easy to apply a transformation like stripping prefixes and suffixes to elements of an array. shopt -s nullglob # if there are no matches, produce an empty list versions=(hsli*.h5) # list matches versions=("${versions[@]#hsli}") # strip prefix versions=("${versions[@]%.h5}") # strip suffix printf ...


2

Without ls, since you're just populating its list with shell globs anyway, you can cut out the middle-man like: glob_hsli()(IFS=.;set +f set -f -- '' hsli*.*.h5 for h5 do case ${h5#*.} in (*[!0-9]*.*|.*|'') : ;; (*) set $h5 "${1:-0}"; shift $((3>>($2>$4)));; esac;done printf "0.%d\n" "${1:?No Match Found!}" ...


0

let me try point something basic . 1. if we already can travel through directory trees , next thing is to search in the current visiting directory . this depends on what style of pattern syntax we want to support . regex is a whole different thing . maybe we are satisfied with the shell built-in pattern grammar . then this will do . for i in * ; do for p ...


5

If your shell supports the ksh ${var/search/replace} form of parameter expansion (ksh93, zsh, mksh, yash, bash): for r1 in *R1*; do r2=${r1/R1/R2} singles=${r1/R1/singles} trimmed1=trimmed$r1 trimmed2=trimmed$r2 sickle pe -f "$r1" \ -r "$r2" \ -o "$trimmed1" \ -p "$trimmed2" \ -s "$singles" done POSIXly, you could do ...


0

Assuming I understand what you're looking for, you could solve it using awk: #!/bin/bash for file in *_R1_*; do file=`basename $file` newFile=`echo $file | awk -F '_R1_' '{print $1}'`_R2_`echo $file | awk -F '_R1_' '{print $2}'` sName=`echo $file | awk -F '_R1_' '{print $1}'`_singles_`echo $file | awk -F '_R1_' '{print $2}'` echo $newFile ...


4

It can be done by GNU ls+awk one-liner: ls -vr *.depot | awk -F- '$1 == name{system ("rm \""$0"\"")}{name=$1}' Explanation: the file names are passed as input to the awk script. The options -vr cause the file names to be sorted as version numbers in reverse order, so e.g. foo-1.9.depot comes after foo-1.10.depot. The awk script stores the first part of ...


-1

$ cat remover #!/usr/bin/perl for(<*depot>){ if(/(\w.*?)-(\d+)(?:\.(\d+))?\.(\d+)-/){ $norm=sprintf("%04d%04d%04d",$2,$3,$4); if($v{$1} and $v{$1} > $norm){ system "rm '$_'\n"; next } if($v{$1} ){ system "rm '$name{$1}'\n";} $v{$1}=$norm; $name{$1}=$_; } } try perl remover and replace print by system ...


1

You can use -exec to create a new bash shell, then manipulate {} inside the shell by passing it as a parameter (it can be accessed as $0 in the new shell). You can remove the .md filetype ending with parameter expansion: find . -name '*.md' -type f -exec bash -c 'pandoc --filter ./filter1.py -o ${0%md}html' {} \;


2

It will work. For example consider a .txt file in a current directory find . -type f -iname "*.txt" -exec basename \{\} .txt \;


2

You can do a few things. head and tail are both spec'd to display the first/last ten lines of a file by default - but if called w/ multiple arguments will do that for all and display the filenames for each. And, of course, for each, you can use the -n[num] argument to specify a number of lines other than the default ten that you would like to display. I ...


2

I would use awk: awk 'FNR==1{print "::::\n"FILENAME"\n::::"}1' *


4

If the output from more is acceptable as it is, just pipe it through cat: more * | cat That will do away with the "more" prompts. Or you can get a bit more control over the display using printf in a one-liner: for fn in *; do printf "::::::\n$fn\n:::::\n"; cat "$fn"; done or as script: for fn in $*; do printf "vvvvvvvvvv\n$fn\n^^^^^^^^^^\n" ...


0

. means current directory. Just run it like this: /usr/bin/foo/bar.sh When you place a . before it looks for: whateverdirectory/yourein/usr/bin/foo/bar.sh


2

The . at the beginning of a directory path means that it is a relative path starting at the current working directory. Without the . it starts at the file system root. So you would have to run /usr/bin/foo/bar.sh without a preceding dot. The dot is only needed when you are located in the same directory as the file to distinguish it from an eventually ...


2

Dots matter. Instead of: ./usr/bin/foo/bar.sh Use: /usr/bin/foo/bar.sh Discussion Consider: ./usr/bin/foo/bar.sh This starts in the current directory, ./, and looks for the subdirectory usr. If the current directory contains no such subdirectory, you will see the message: bash: ./usr/bin/foo/bar.sh: No such file or directory By contrast, ...


3

With zsh: for d (subdir*(/)) mixb $d/*.(dat|d01)([1]) The bash equivalent would be something like: shopt -s nullglob extglob for d in subdir*/; do [ -L "${d%/}" ] && continue set -- "$d"*.@(dat|d01) [ "$#" -eq 0 ] || mixb "$1" done


2

Which Unix? It will be quite difficult to make it so that file creation fails when an "invalid" name is passed to it, but there are some platform-specific ways of doing this. First of all, there is inotify and its relatives, which can at least observe the filesystem and notify you immediately when a file is created. I'm not sure you can actually stop the ...


0

cd /home/sub1/samples for file in aaa*/*_novoalign.bam; do target_file="${file%_novoalign.bam}_output.txt" samtools depth -r chr9:218026635-21994999 <"$file" >"$target_file" done


0

You need to remove the path and then the extension you don't want: base=${file##*/} newfile=${base%_novoalign.bam}_output.txt Then use: ... > "/home/sub2/sub3/$newfile" Check out Shell Parameter Expansion here for other options: GNU Bash Documentation


1

Try: perl -i.bak -wpe 's/SEARCH_FOR/$ARGV/g' *.txt $ARGV contain the name of current file when reading from <>. Because you using -i switch, you don't have to worry about security implication when running perl with -n or -p switch.


0

You can try something like this. for i in `ls *.sh`; do echo "$i">>$i.new; cat $i.new $i >>$i.new;done What this does it basically list the files and store the filenames. Write those names to a new file and cat it with the original file. You can also move the .new file to the original filename if required. This is not the most elegant option, ...


1

A shell script with a "here document" will handle this nicely: #!/bin/bash for NAME in t? do ex -s $NAME << END_EDITS 1s/TestRelation/$NAME/ w! q END_EDITS done Not knowing what your files are named, I chose "t1", "t2", "t3", etc as file names. You will have to generate file names as appropriate. The "for" loop gives shell variable NAME the value ...


0

If you just wish to do it in shell, use find command to list files and then basename command to get the file name and use sed to replace the string for i in $(find . -type f); do fname=$(basename $i); sed -i "s/TestRelation/${fname}/" $fname; done Well basename and find is not required and you can just use ls if you are sure of only files in a particular ...


5

Use bash arrays: pa=("/home/user/folder1" "/home/user/fol der2") find "${pa[@]}" -name '*xy*' ... Discussion Observe that the \ does not do what one hopes: $ pa="/home/user/folder1 /home/user/fol\ der2" $ printf '%s\n' $pa /home/user/folder1 /home/user/fol\ der2 The \ in the definition of pa becomes a literal character in the string, not an escape. ...


0

Use an array variable and put one path value in each element of the array. If that really isn't possible, another approach would be to use something other than space as your item separator. p[0]="hello" p[1]="happy" p[2]="world" echo "p[0]=${p[0]}, p[1]=${p[1]}, p[2]=${p[2]}" for p in "${p[@]}" do echo "p=$p." done Etc.



Top 50 recent answers are included