-3

I have 8 files in a folder. If any of these files has 2 lines I need to remove the second line from that file. If a file has more than 2 lines then then I do not need to do anything to it.

  • Please include what you tried next time. You are just asking someone else to make something for you. – PNDA Apr 25 '15 at 4:36
0

With find and sed:

find -type f -exec sh -c '[[ $(sed -n $= $0) -eq 2 ]]' {} \; -exec sed -i '1!d' {} +

Note: find should be tweaked with PATH and -maxdepth depending on the directory structure.

  • Hi this is working fine and out put is as i need... thank you... can i specify what files to reed i need to perform this only on file name starts with " ELL-* " where can i add this in above line Thank you so much for your help – deepuu Apr 22 '15 at 4:59
  • @deepuu You might want to take a look at the man page of find. You can add find -name "ELL-*" -type f ... to only process given files. – FloHimself Apr 22 '15 at 5:49
  • hi i tried this find -name "$PMTargetFileDir/mfg/ELL-" -type f -exec sh -c '[[ $(sed -n $= $0) -eq 2 ]]' {} \; -exec sed -i '1!d' {} + but it is not going to that Path tosearch files with ELL- and i am very new to Unix – deepuu Apr 22 '15 at 7:14
  • You should use find /your/target/directory -name "ELL-*" -type f ... – FloHimself Apr 22 '15 at 7:27
  • Thanks Team It was fine and all working as expected....Thanks a lot – deepuu Apr 22 '15 at 11:55
1

try this script it works fine.

for file in $(ls *.txt)
do
    LINENUMB=`wc -l $file | cut -f1 -d' '`
    if [[ $LINENUMB == 2 ]]; then
    sed -i '$d' $file
    fi
done
  • hi i tried running this it giving erroe as command not found – deepuu Apr 22 '15 at 4:17
  • @deepuu it works fine for me, could you please show me that error!! – Gimbo Apr 22 '15 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.