Skip to main content
added 284 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52

Edit: 1st:

find -type f -exec grep -I -q . {} \; -exec sh -c 'echo -e "$(basename "$0") {}"' {} \; | sort | cut -d ' ' -f2 | xargs sed -Ee 's/(^.{3})(.*)/\1/g' 

I tried for 2 hours and thisgrep -I -q . {} \; will find all the best solution I got:text files only, not binary files like images,etc.`

2nd:

Edit:

I tried for 2 hours and this the best solution I got:

Edit: 1st:

find -type f -exec grep -I -q . {} \; -exec sh -c 'echo -e "$(basename "$0") {}"' {} \; | sort | cut -d ' ' -f2 | xargs sed -Ee 's/(^.{3})(.*)/\1/g' 

grep -I -q . {} \; will find all the text files only, not binary files like images,etc.`

2nd:

added 13 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52

Edit:

I tried for 2 hours and this the best solution I got:

My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;' {} \; | sort | while read -r line do source file_s echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

The second -exec command is as follows:

i="$(basename "$0")"; will get the basename and will write into variable i.

echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.

sort will sort the file names.

while read -r line do source file_s echo -e ${map["$line"]} done 

Will read line by line and source the file file_s. Then will print the first 3 characters.


You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

I tried for 2 hours and this the best solution I got:

My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;' {} \; | sort | while read -r line do source file_s echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

The second -exec command is as follows:

i="$(basename "$0")"; will get the basename and will write into variable i.

echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.

sort will sort the file names.

while read -r line do source file_s echo -e ${map["$line"]} done 

Will read line by line and source the file file_s. Then will print the first 3 characters.


You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

Edit:

I tried for 2 hours and this the best solution I got:

My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;' {} \; | sort | while read -r line do source file_s echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

The second -exec command is as follows:

i="$(basename "$0")"; will get the basename and will write into variable i.

echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.

sort will sort the file names.

while read -r line do source file_s echo -e ${map["$line"]} done 

Will read line by line and source the file file_s. Then will print the first 3 characters.


You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

added 693 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52

I tried for 2 hours and this the best solution I got:

My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;echo "$i"'file_s;' {} \; | sort | while read -r line do source file_s echo $line echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

The second -exec command is as follows:

i="$(basename "$0")"; will get the basename and will write into variable i.

echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.

sort will sort the file names.

while read -r line do source file_s echo -e ${map["$line"]} done 

Will read line by line and source the file file_s. Then will print the first 3 characters.

 

You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

I tried for 2 hours and this the best solution I got:

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;echo "$i"' {} \; | sort | while read -r line do source file_s echo $line echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

I tried for 2 hours and this the best solution I got:

My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.

#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;' {} \; | sort | while read -r line do source file_s echo -e ${map["$line"]} done 

The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.

The second -exec command is as follows:

i="$(basename "$0")"; will get the basename and will write into variable i.

echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.

sort will sort the file names.

while read -r line do source file_s echo -e ${map["$line"]} done 

Will read line by line and source the file file_s. Then will print the first 3 characters.

 

You can't use head because head prints the content of the file not the file names.

You can use:

find . -type f -exec basename {} \; | sort | cut -c 1-3 

Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.

find . -type f -exec basename {} \; | sort | cut -b 1-3 

It will get first three characters.

You can use:

find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g' 

It will sort the files and then will match starting three characters and will only print them.

Note: All of these commands will consider space and tab as one character.

added 693 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading
added 173 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading
added 173 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading
added 119 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading
added 86 characters in body
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading
Source Link
Prvt_Yadav
  • 6k
  • 7
  • 39
  • 52
Loading