-2

Please excuse me for my amateur language.

I have written a simple one line code to cat out content I want from a huge text file:

cat file | grep "value" | cut -f1 -d":"

It out put lines of paths of file from there on.

I want it to go on doing this:

  1. cd into the paths one line at a time.
  2. each time after cd run this command: ls | grep .fileformat
  3. Then let me run 3 commands I choose to, if I press [return] with no value it will ignore and listen for the next command. After three is done it will go on.
  4. cd into the directory of the next line and repeating until the last line.

Sorry I couldn't figure this later part out as I didn't know where to start googling even.

Thank you for looking!

Edit 1: output of my initial command gives paths like this:

/home/user/path/to/file

So there is no ~, it should be able to cd into them no problem?

Asmodean
  • 29
  • 4
  • How do you choose the commands in step 3? Are you sure you have to cd into the various directories? What do you do with the output from step 2? `ls | grep` has a few issues, and `.fileformat` almost certainly matches things you didn't intend to match (`.` is a regex special character that matches everything). – Benjamin W. Sep 03 '21 at 03:08
  • @BenjaminW. I am just going to be rm and cat the files listed by ls | grep .mkv for example. I tried the ls command, it worked flawlessly for me – Asmodean Sep 03 '21 at 03:12
  • 1
    Instead of `cat file | grep value` do `grep value file`. What is paths in 1? If you cd in a sub-sell () it will revert to known directory, otherwise you have to keep track unless they are absolute paths. – Allan Wind Sep 03 '21 at 03:12
  • @AllanWind Apologies I know not what known directs are, but the out put of my command, (however inefficient as you pointed out) gives this: /home/user/file/path. So the paths are "absolute" I suppose. No like ~ or soft links – Asmodean Sep 03 '21 at 03:15
  • sed is often a good option instead of a grep + cut per below. – Allan Wind Sep 03 '21 at 03:22
  • `ls | grep .fileformat` might be expressed more directly as ls *.fileformat` unless it's a huge directory and * would overflow. – Allan Wind Sep 03 '21 at 03:30
  • _I couldn't figure this later part_ : What exactly do you mean by "this later part"? You are doing 4 things with each line, the last one consisting of two parts (cd and redo). Where exactly are you stuck? – user1934428 Sep 03 '21 at 06:16

2 Answers2

0

A slightly modified version of Allan Wind's answer that does what I THINK the OP wants (tried to reply as a comment to Allan but couldn't figure out code formatting properly):

sed -n '/value/s/\([^:]*\):.*/\1/p' file | while read d
do
  echo -e "Entering $d"
  cd $d
  ls | grep .fileformat
  for i in {1..3}; do
    echo -e "Type your command #${i}:\n"
    read -p '>' input < /dev/tty
    [ -z "$input" ] && echo -e "\nDoing nothing this time...\n" && continue
    eval "$input"
  done
done

(Usual caveats of reading interactively + using eval being dangerous)

-1

Here is the skeleton of an answer to help you ask a better question:

sed -n '/value/s/\([^:]*\):.*/\1/p' file | while read d
do
  (
     cd d
     ls | grep .fileformat
     read -p  "3 commands? " c
     [ -z "$c" ] && continue
     cd d
     eval "$c"
  )
done

(Usual caveats of reading interactively + using eval being dangerous)

Allan Wind
  • 11,844
  • 4
  • 24
  • 32
  • Thanks, it will take me some time to digest that. Hopefully I won't bother the forum again. – Asmodean Sep 03 '21 at 03:22
  • No worries, we are here to help, just mark it as answered so we know you are set. eval is scary, as it allows you to do anything, so if it was me, I would seek to change the problem... is there limited set of commands? If so then you write a function for each and have the program select on of the defined functions. Even better can you automate the "look at the output of ls and decide what commands you should run"? – Allan Wind Sep 03 '21 at 03:28