Skip to main content
added 73 characters in body
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

out.txt in each subfolder with first line of input.txt in that particular subfolder

find + sed solution:

find . -type f -name input.txt -exec sh -c 'out_fn="${1%/*}/out.txt"; sed -n "1p" "{}" > "$out_fn"' _ {} \;
  • out_fn="${1%/*}/out.txt" - path to output filename in format /path/to/subfolder/out.txt

    out_fn="${1%/*}/out.txt" - path to output filename in format /path/to/subfolder/out.txt

  • sed -n "1p" "{}" - extracts the 1st line from the input file {}

out.txt in each subfolder with first line of input.txt in that particular subfolder

find + sed solution:

find . -type f -name input.txt -exec sh -c 'out_fn="${1%/*}/out.txt"; sed -n "1p" "{}" > "$out_fn"' _ {} \;
  • out_fn="${1%/*}/out.txt" - path to output filename in format /path/to/subfolder/out.txt

out.txt in each subfolder with first line of input.txt in that particular subfolder

find + sed solution:

find . -type f -name input.txt -exec sh -c 'out_fn="${1%/*}/out.txt"; sed -n "1p" "{}" > "$out_fn"' _ {} \;
  • out_fn="${1%/*}/out.txt" - path to output filename in format /path/to/subfolder/out.txt

  • sed -n "1p" "{}" - extracts the 1st line from the input file {}

Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

out.txt in each subfolder with first line of input.txt in that particular subfolder

find + sed solution:

find . -type f -name input.txt -exec sh -c 'out_fn="${1%/*}/out.txt"; sed -n "1p" "{}" > "$out_fn"' _ {} \;
  • out_fn="${1%/*}/out.txt" - path to output filename in format /path/to/subfolder/out.txt