Sponsored Content
Top Forums Shell Programming and Scripting Trim whitespace and add line break Post 302103936 by Glenn Arndt on Monday 22nd of January 2007 11:56:07 AM
Old 01-22-2007
Here's a crude way using awk:
Code:
awk '{ gsub("^ *","",$0); gsub(" *$","",$0); gsub("  *"," ",$0); gsub(" ","\n",$0); print $0 }' $file

Removes leading and trailing whitespace, replaces multiple consecutive spaces with one space, then replaces the remaining spaces with newlines.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies

2. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

3. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

4. Shell Programming and Scripting

Trim a new line

Okay, I am trying to make a bash script to get a certain domains IP address (my home ip). My home is on a DHCP lease from my ISP, so I cannot always trust the IP address to remain constant. This is what I have so far for it: alias ip-home="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 |... (5 Replies)
Discussion started by: tnanek
5 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

7. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

8. UNIX for Dummies Questions & Answers

insert whitespace (tab) at start of each line

Hi all, I have this: begin data; dimensions nind=168 nloci=6; info BDT001.4 ( 1 , 1 ) ( 1 , 12 ) BDT003.4 ( 1 , 1 ) ( 12 , 12 ) BDT007.4 ( 1 , 1 ) ( 12 , 12 ) BDT009.4 ( 1 , 32 ) ( 12 , 22 ) etc, etc And need this: begin data; dimensions nind=168 nloci=6; info ... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

9. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

10. Shell Programming and Scripting

How to replace a whitespace with line break?

Hello, I am a beginner in Unix and I'm learning how to program by myself. I am trying to create a shell script that is able to take in either a file containing input or by standard input, and replacing every whitespace with a new line break. so basically the input would be This <SP> is... (10 Replies)
Discussion started by: fozilla
10 Replies
Ns_String(3aolserver)					   AOLserver Library Procedures 				     Ns_String(3aolserver)

__________________________________________________________________________________________________________________________________________________

NAME
Ns_Match, Ns_NextWord, Ns_StrCaseFind, Ns_StrCopy, Ns_StrDup, Ns_StrNStr, Ns_StrToLower, Ns_StrToUpper, Ns_StrTrim, Ns_StrTrimLeft, Ns_Str- TrimRight, Ns_StringPrint, ns_strcopy, ns_strdup - library procedures SYNOPSIS
#include "ns.h" char * Ns_Match(char *a, char *b) char * Ns_NextWord(char *line) char * Ns_StrCaseFind(char *s1, char *s2) char * Ns_StrCopy(char *str) ns_strcopy(char *str) char * Ns_StrDup(char *str) ns_strdup(char *str) char * Ns_StrNStr(char *pattern, char *expression) char * Ns_StrToLower(char *string) char * Ns_StrToUpper(char *string) char * Ns_StrTrim(char *string) char * Ns_StrTrimLeft(char *string) char * Ns_StrTrimRight(char *string) void Ns_StringPrint(char *string) _________________________________________________________________ DESCRIPTION
Ns_Match(a, b) Compare the beginnings of two strings, case insensitively. The comparison stops when the end of the shorter string is reached. Return NULL if no match, b if match. Ns_NextWord(line) Return a pointer to first character of the next word in a string; words are separated by white space. The returned pointer points into the original string. For example, Ns_NextWord("abc def") returns a pointer to the 'd' in that string. Ns_StrCaseFind(s1, s2) Locate the first occurrence of substring s2 within string s1 in a case-insensitive manner. The terminating ' ' characters are not compared. Returns a pointer that points into s1. Uses strstr(3) to do its work. Ns_StrCopy(str) ns_strcopy(str) Copy a string or NULL value using Ns_Malloc. The Ns_StrCopy function is identical to the Ns_StrDup function but allows for the string parameter to be NULL, in which case Ns_StrCopy does nothing and returns NULL. Ns_StrDup(str) ns_strdup(str) Copy a string using Ns_Malloc. The Ns_StrDup function calls Ns_Malloc to allocate enough memory to make a copy of the given string. Ns_StrNStr(pattern, expression) Search through pattern for expression, case insensitively. Return a pointer that points to where the match begins within pattern, or NULL if expression is not contained in pattern. Ns_StrToLower(string) Convert string to lowercase. Returns pointer to original string. Original string will be modified. Ns_StrToUpper(string) Convert string to uppercase. Returns pointer to original string. Original string will be modified. Ns_StrTrim(string) Trim leading and trailing white space from string. A pointer to the trimmed string will be returned, which will be in the original string. Do not lose your original pointer to the string if you later need to free it. Ns_StrTrimLeft(string) Trim leading white space from string. A pointer to the trimmed string will be returned, which will be in the original string. Do not lose your original pointer to the string if you later need to free it. Ns_StrTrimRight(string) Trim trailing white space from string. The original string pointer will be returned, but the string will have been modified: a ' 's will have been placed right after the last non-whitespace character of the string. Ns_StringPrint(string) Print a string to stdout. SEE ALSO
nsd(1), info(n) KEYWORDS
AOLserver 4.0 Ns_String(3aolserver)
All times are GMT -4. The time now is 10:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy