Bash script

I have a data file and there are some unwanted blank lines. How to write a bash script , so that I can generate a new data file without these unwanted blank line ?
Thank you.

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

On top, your problem has been solved umpteen times in these forums. Did you try searching for it?

I have a data files which consists of say almost 104 lines. I need to delete the 16 th,27th,38th,49th,60th,71,82,93.....etc lines from the data. How to write a bash script for this? I am a new linux user. Currently using vim-editor.

Hi,

There are a number of approaches, as an example you could test with.

fred@test # cat file_name | grep -v '^$'

Or you could use;

fred@test # sed '/^$/d' file_name

There are many options, what have you tried?

Gull04

Hi,
Could you please tell me how to use do..loop for deleting these specific lines using bash script?

The best approach IMO may be..

awk NF file

Since that will also delete lines that are strictly not empty, but that only contain spaces and TABS..

1 Like

Hi,
I have a file and I need to edit that file by deleting certain lines. I am interested in deleting the line number 16,27,38,49,60....etc from my original data file and would like to make a edited version of my file. For this I have used following code:

#!bin/bash
 loop.txt= $1
 loop1.txt=$2
 line=16
 while read line [$line -eq 16 && $line -le 64082];
 do
 >sed -line loop.txt
 echo $line>>"$loop1.txt"
 ((line=line+11))
 done<"$loop.itxt"

But its not working for me. How to write a bash script for this?

Try:

awk '(NR+6)%11' file > newfile

GNU sed:

sed '16~11d' file > newfile

Bash/ksh93/zsh:

while IFS= read -r line
do
  (( (++nr+6)%11 )) || continue
  printf "%s\n" "$line"
done < file > newfile

I agree with Scrutinzer, it's the way to go.