PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, May 15, 2022

[FIXED] How can I cut off tail of each line in the text file in all subfolders with shell script?

 May 15, 2022     shell, ubuntu     No comments   

Issue

I have this project,

Parent
  |-ChildA
  |-ChildB
  |- ....
  |-ChildZ

and each child directory contains requirements.txt that has python package information like this,

packageA==0.1
packageB==5.9.3
...
packageZ==2.9.18.23

I want to cut off all version information so that output file will be,

packageA
packageB
...
packageZ

I am trying,

cat requirements.txt | grep "==" | cut -d "=" -f 1

but it does not iterate all subdirectories and does not save. How can I make it? Thanks!
*I am using ubuntu20.04


Solution

In order to Execute the command on all the requirements.txt files, you'll need to iterate through all the Child directories, You can do so using this simple script:

#!/bin/sh

for child in ./Child* ; do
        cat "$child/requirements.txt" | grep "==" | cut -d "=" -f 1
done

Now if you wish to "save" the new version of each file, you can just redirect each command output to the file using the > operator. Using this operator will overwrite your file, so I suggest you redirect the output to a new file.

Heres the script with the redirected output:

#!/bin/sh

for child in ./Child* ; do
        cat "$child/requirements.txt" | grep "==" | cut -d "=" -f 1 > $child/cut-requirements.txt
done


Answered By - Pietro Pezzi
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing