- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Removing spaces between lines
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 01:40 AM
01-10-2008 01:40 AM
I am having one file say abc.txt. File has two fields.Entries of files :
Prefix description
"202xxxx" This is hhhhh tatat.
"303nnnnn" This is 2nd line.
"606hhhh" This is 3rd line.
......
.... and so on.
Between two lines many blank lines are there. I want to remove all blank line.
Logic i have written is:
Code:
****************************************
var1=`wc -l abc.txt.txt | cut -f1 -d' '`
#This will take total no of lines in file.
count=1
while read line
do
length=`echo $line | wc -w`
if [ $length -eq 0 ]; then
echo " NULL LINE "
else
`echo $line >> abc_final.txt`
fi;
if [ $count -eq $var ]; then
echo " Breaking loop "
break;
fi;
count=`expr $count + 1`
done < abc.txt
******************************************
Can some one suggest any simple and effective way to acieve this?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 02:01 AM
01-10-2008 02:01 AM
Re: Removing spaces between lines
You can use awk to achieve this:
cat abc.txt | awk '$0!~/^$/ {print $0}' > abc.txt.new
regards,
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 02:06 AM
01-10-2008 02:06 AM
Re: Removing spaces between lines
Check this from http://sed.sourceforge.net/sed1line.txt:
# delete ALL blank lines from a file (same as "grep '.' ")
sed '/^$/d' # method 1
sed '/./!d' # method 2
example:
$sed '/./!d' yourfile >> newfile
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 02:07 AM
01-10-2008 02:07 AM
Re: Removing spaces between lines
grep -v "^$" abc.txt >newfile
would also be possible.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 03:21 AM
01-10-2008 03:21 AM
Re: Removing spaces between lines
Simply do sed '/^$/d' abc.txt > def.txt that should suffice.
where abc.txt will have quiet few new lines in it, like
abc
qasd
sd
EOF
def.txt will be
abc
qasd
sd
EOF
Thanks,
Srikanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 05:02 AM
01-10-2008 05:02 AM
SolutionThe problem with reqular expressions like:
/^$/
...is that this assumes that a "blank" line consists only of a newline character without any intervening spaces and/or tabs.
If you wish to use 'sed':
# sed '/^[ ]*$/d' file
...where the expression in brackets consists of a space followed by a tab character, will work. Some 'sed' variations (but not HP's) allow a '\t' in lieu of typing the actual TAB.
Otherwise, I prefer:
# perl -ne 'print unless /^\s*$/' file
The '\s' represents "whitespace" which is defined to include both spaces and tabs. Hence if a line consists of zero or more characters of whitespace only, it is skipped.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 12:13 PM
01-10-2008 12:13 PM
Re: Removing spaces between lines
ex -s file <
x
EOF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 10:42 PM
01-10-2008 10:42 PM
Re: Removing spaces between lines
Appologies for very delay in responding.
Both solutions given by James is working for me. I am choosing perl out of that.
Thanks a lot to all for their replies.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2008 12:59 AM
01-11-2008 12:59 AM
Re: Removing spaces between lines
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2008 04:24 AM
01-11-2008 04:24 AM
Re: Removing spaces between lines
cat $MYFILE | ssp | rmnl >> abc_final.txt
Note that if the incoming file is badly formatted with space-filled lines (which look blank but actually have blank characters) then additional steps will be needed to remove those 'looks-blank' lines.
Bill Hassell, sysadmin