awk move line up if not matching pattern - awk

I am new to awk and sed. I have the following lines and want to move the line up if it does not match pattern.
File:
company name
address line
city, state, zip
extra info
company name
address line
city, state, zip
extra info
company name
address line
city, state, zip
extra info
... and it goes on like that
want to use pattern matching 'company name' . if the line does not have 'company name' move the line up.
Desired output:
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
... and continue on
Thanks for any help

Here us how to do it with awk
awk '{printf "%s"(NR%4?", ":RS),$0}' file
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
For every 4 line, use RS, else use ,
Or as Jaypal suggested:
awk '{ORS=(NR%4?", ":RS)}1' file

$ awk '{printf "%s%s", (/company name/?rs:", "), $0; rs=RS} END{print ""}' file
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info

paste is a good tool for this job (assuming you are ok with , as a delimiter instead of , followed by space)
<file paste -d',' - - - -
company name,address line,city, state, zip,extra info
company name,address line,city, state, zip,extra info
company name,address line,city, state, zip,extra info
Alternately
<file paste -s -d',,,\n'

You could try this awk command also,
awk 'BEGIN{RS="company"}{ gsub (/\n/,", ");} NR>=2 {sub (/, $/,""); print RS$0}' file
Example:
$ cat file
company name
address line
city, state, zip
extra info
company name
address line
city, state, zip
extra info
company name
address line
city, state, zip
extra info
$ awk 'BEGIN{RS="company"}{ gsub (/\n/,", ");} NR>=2 {sub (/, $/,""); print RS$0}' file
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info
company name, address line, city, state, zip, extra info

Related

Is there a way to quit the current function based on a condition but not end the program

I have 2 class methods. One method 'accountExistance()' checks if an account already exists by finding a duplicate username in a database and the other method 'customerDetails()' writes the user registration details to the database. My customerDetails() method calls the 'accountExistance() method and if there is a duplicate username, which is checked within 'accountExistance' method, I return False and back in the initial method, if the return value is False 'sys.exit()' occurs.
The problem lies because I would rather not quit the program as its running a tkinter gui, allowing the user to change the registration details. Rather, is there a way of instead of quitting I can cancel the execution where it is and not submit duplicate values to a database.
To note, customerDetails() is called from another file upon the user pressing registration where stringVars() from entry boxes are fed into the class method. Also, the indentation appearance is not formatted properly on here but I have produced a minimal example for logic, and have cut out some lines of code.
class Database():
def __init__(self):
Database.phoneNumber = ""
def accountExistance(email, phoneNumber):
conn=sqlite3.connect("system.db")
cur=conn.cursor()
emailExist = cur.execute("SELECT count(email) FROM customerDetails WHERE email = ?", (email,)).fetchall()
conn.commit()
print(emailExist)
phoneExist = cur.execute("SELECT count(phone) FROM customerDetails WHERE phone = ?", (phoneNumber,)).fetchall()
print(phoneExist)
conn.commit()
if emailExist or phoneExist != 0 :
tk.messagebox.showinfo("Error", "This email and/or phone number is associated with an account")
return False
sys.exit()
#else:
#return True
#INSERT SIGN UP DETAILS
def customerDetails(forename, surname, dob, address, city,
county, postcode, phone, email, password,verified, gender):
print(forename, surname, dob, address, city, county, postcode,
postcode, phone, email, password, verified, gender)
test = Database.accountExistance(email, phone)
if test == False:
sys.exit()
age = 0
conn=sqlite3.connect("system.db")
cur=conn.cursor()
cur.execute("INSERT INTO customerDetails VALUES
(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)",(forename, surname, dob, address, city,
county, postcode, phone, email, password, verified, age, gender))
conn.commit()
conn.close()
Just change your condition to the following:
test = Database.accountExistance(email, phone)
if test == False:
return # Stops the execution of the function
Also, if test is necessarily either True or False, you can check its value directly:
if not test:
is the same as
if test == False:

AWK multiline match

I have read more topics about my problem but nothing has resolved that!
I have these text in my_text file:
Address dlkjfhadvkahvealkjvhfelkafver
Phone 4752935729527297
Discription fkdshkhglkhrtlghltkg
Misc 5897696h8ghgvjhgh578hg
Address klsfghtrgjgjktsrljgsjgm
Phone 5789058309809583
Discription dskjfvhfhgjvnwmrew
Misc h09v3n3vt7957jt795783hj
.....
.....
.....
And I want to filter this file data by 3 (or more) line value such as Address, Phone, Misc.
I test awk '/Address/,/Phone/,/Misc/' my_text but error!
You need to use or | operator.
Matched line:
awk '/Address|Phone|Misc/{ print $0 }' your_text
Result:
Address dlkjfhadvkahvealkjvhfelkafver
Address dlkjfhadvkahvealkjvhfelkafver
Phone 4752935729527297
Misc 5897696h8ghgvjhgh578hg
Address klsfghtrgjgjktsrljgsjgm
Phone 5789058309809583
Misc h09v3n3vt7957jt795783hj
if you print $1 you get Address, phone or what you matched. And $2 will print your values only.

Parse Dataframe string column into Street, City, State & Zip code

I am trying to break up the following fixed string into several columns as street ,city, state & zip code. Is it possible to do this in SQLDF via the INSTR & Subtr method?
Sample Address String. The difficult part is the NV and zip code parsing.
727 Wright Brothers Ln, Las Vegas, NV 89119, USA
I am able to parse the city/street information using sqldf/instr but unable to parse the final two values for state/zip code
parsed_tweetAddressdf <- sqldf("SELECT lon, lat, result, substr(result,0,instr(result,',')) AS street, substr(result,instr(result,',')+1,instr(result,',')-1) AS city from tweetAddressdf")
Here are some alternatives. They all use instr and substr as required by the question although the third also writes out the data and reads it back in (in addition to using instr and substr). Notes at the end point out that it is also easy to do this in plain R or using read.pattern in gsubfn.
1) Assume state, zip and country fields are fixed width With only one sample record it is impossible to know what your general case is but if we assume that every record ends in SS ZZZZZ, USA where SS is the two letter state abbreviation and ZZZZZ is a 5 digit zip then this works:
DF <- data.frame(v = "727 Wright Brothers Ln, Las Vegas, NV 89119, USA")
library(sqldf)
sqldf("select
substr(v, 0, instr(v, ',')) street,
substr(v, instr(v, ',') + 2, length(v) - 16 - instr(v, ',')) city,
substr(v, -13, 2) state,
substr(v, -10, 5) zip
from DF")
giving:
street city state zip
1 727 Wright Brothers Ln Las Vegas NV 89119
2) Separate strictly based on commas (except state/zip) This approach avoids certain assumptions in (1) at the expense of additional complication. It takes the first two comma separated fields, the 2 character state and everything after that to the next comma as the zip.
It uses a triple nested select. The innermost select denoted a parses the input string into: street and a.rest. The next one proceeding outward denoted b returns the street already parsed from a, and parses a.rest into city and the b.rest. The outermost one returns the street and city already parsed plus it returns the two state characters in b.rest and everything beyond them in b.rest to the next comma as zip.
library(sqldf)
sqldf("
select
street,
city,
substr(b.rest, 1, 2) state,
substr(b.rest, 4, instr(b.rest, ',') - 4) zip
from (
select
street,
substr(a.rest, 0, instr(a.rest, ',')) city,
substr(a.rest, instr(a.rest, ',') + 2) rest
from (select
substr(v, 0, instr(v, ',')) street,
substr(v, instr(v, ',') + 2) rest
from DF) a) b
")
giving:
street city state zip
1 727 Wright Brothers Ln Las Vegas NV 89119
3) read.csv.sql If it's OK to write it out and read it back in then we can use read.csv.sql, a wrapper around sqldf. Although the question did not ask for it, this one also parses out the country:
write.table(DF, "addresses.csv", row.names = FALSE, col.names = FALSE,
sep = ",", quote = FALSE)
read.csv.sql("addresses.csv", header = FALSE, sql =
"select V1 street,
V2 city,
substr(V3, 2, 2) state,
substr(V3, 4) zip,
V4 country
from file")
giving:
street city state zip country
1 727 Wright Brothers Ln Las Vegas NV 89119 USA
Note 1: This is also easy in plain R.
dd <- read.table(text = as.character(DF$v), sep = ",",
col.names = c("street", "city", "state_zip", "country"))
transform(dd,
state = substring(state_zip, 2, 3),
zip = substring(state_zip, 4))[c(1, 2, 5, 6, 4)]
giving:
street city state zip country
1 727 Wright Brothers Ln Las Vegas NV 89119 USA
Note 2: It is even easier using read.pattern from gsubfn:
library(gsubfn)
pat <- "(.*), (.*), (..) (.*), (.*)"
read.pattern(text = as.character(DF$v), pattern = pat,
col.names = c("street", "city", "state", "zip", "country"))
giving:
street city state zip country
1 727 Wright Brothers Ln Las Vegas NV 89119 USA

awk replace serialized number lines and move up other lines

I have a file that has the following format
1 - descrio #944
name
address
2 - desanother #916
name
address
3 - somedes #957
name
address
and i want to get the output as,
Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address
With awk
awk 'NR%3 == 1{sub(/^.*#/, "Usercode #")};{ORS=NR%3?", ":"\n"};1' file
Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address
For a variable number of rows
awk -v RS='(^|\n)[[:digit:]]+[[:blank:]]*-[[:blank:]]*' '{sub(/\n$/, "");
gsub(/\n/, ", "); printf "%s", $0""RT}END{print ""}' file
If you do not have # in any of your descriptions, try:
sed -e 's/.*#/Usercode #/;N;N;s/\n/, /g' input
You may try this command also,
$ paste -d'~' - - - < ccc | sed 's/^[^#]*/Usercode /g;s/~/, /g'
Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address

How to remove blank Space in rdlc

I am working with Rdlc report. I have these fields:
First name : fn_data
Address1 : add1_Data
Address2 : add2_data
City, state, zip : city_data
If any one of these fields is empty, it should not show the blank space. For example (expected output)
First name,
Address1,
City, state, zip
But, as show in the above image, I am getting this:
First name,
Address1,
........................<-(blankspace is showing here)
City, state, zip
I tried changing Visiblity -> Expression -> ==IIF(IsNothing(Fields!add2_data.Value),False,True)
How can I remove this Blanck Space??
In Visibility.Hidden property you have to specify if the object must be hide or not.
So you can use:
=IIf(IsNothing(Fields!YourData.Value), True, False)
If you're using Table/Tablix you have to set Visibility property of your TABLEROW.